CrazyEngineers
  • is it possible for a class in java to have no constucror...

    Updated: Oct 26, 2024
    Views: 1.1K
    I am finding no constructor while using javap tool for my java class...
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Anoop Kumar

    MemberJun 19, 2012

    Didn't used javap command..
    but default constructor always added to class file, either it written in source code or not. did you tried for it JD java decompiler to generate source file from class file.

    If you have any doubt just call super(); as first line in subclass *constructor without having any constructor in super class. see what will happen!!!!😀
    Are you sure? This action cannot be undone.
    Cancel
  • Neeraj Sharma

    MemberJun 19, 2012

    No need at all. There is always a default constructor as follows even if you dont declare yours,
    It has following structure.
    public class_name
    {
    }
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberJun 19, 2012

    #-Link-Snipped-# I am getting the default constructor getting displayed for my java class using javap. Can you please share your java class along with javap command results?
    Are you sure? This action cannot be undone.
    Cancel
  • Vikash Verma

    MemberJun 19, 2012

    getting following error while using super(); in subclass constructor...😎

    C:\Users\VIK\Desktop>javac *.java
    B.java:6: cannot find symbol
    symbol : constructor A()
    location: class A
    super();
    ^
    1 error
    Are you sure? This action cannot be undone.
    Cancel
  • Vikash Verma

    MemberJun 19, 2012

    I am getting no constructor using javap tool in my, class here goes the output of my class... 😎
    [​IMG]
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJun 19, 2012

    your code please...
    Are you sure? This action cannot be undone.
    Cancel
  • Vikash Verma

    MemberJun 19, 2012

    #-Link-Snipped-# does it always happen that for each and every java program javap tool will display at least one constructor...😎
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJun 20, 2012

    Vikash Verma
    #-Link-Snipped-# does it always happen that for each and every java program javap tool will display at least one constructor...😎
    Sure it will show you all class members using command javap -private <ClassName>
    If your code is like this one
    class super1{
    }
    public class HelloWorld extends super1{
        HelloWorld (String s)    {
              s="ABC"    ;
            }
    public static void main(String[] args) {
            System.out.println("Hello, World");
        }
    }
    
    Then compile the code and use javap you will get following output
    C:\Documents and Settings\<User>\My Documents\Downloads>javap -private Hell
    oWorld
    Compiled from "HelloWorld.java"
    public class HelloWorld extends super1{
        HelloWorld(java.lang.String);
        public static void main(java.lang.String[]);
    }
    
    For class super1.
    C:\Documents and Settings\<user>\My Documents\Downloads>javap -private supe
    r1
    Compiled from "HelloWorld.java"
    class super1 extends java.lang.Object{
        super1();
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Vikash Verma

    MemberJun 21, 2012

    This is what my code is displaying...😎

    C:\Users\VIK\Desktop>javac B.java

    C:\Users\VIK\Desktop>javap B
    Compiled from "B.java"
    class B extends java.lang.Object{
    public static void main(java.lang.String[]);
    }
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberJun 21, 2012

    Vikash Verma
    This is what my code is displaying...😎

    C:\Users\VIK\Desktop>javac B.java

    C:\Users\VIK\Desktop>javap B
    Compiled from "B.java"
    class B extends java.lang.Object{
    public static void main(java.lang.String[]);
    }
    And can you Please tell what is your code? B.java?
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberJun 21, 2012

    I was just trying something and came to know that may be your B.java has declared constructor as "private" If you have its visibility as "private" then following command "javap B" will not show the private constructor. In order to view it you will have t explicitly run the command as "javap -private B".
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJun 21, 2012

    my JDK giving error while running javap B.
    While "javap -private B". is working fine.
    #-Link-Snipped-# , can you confirm this.
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberJun 21, 2012

    ianoop
    my JDK giving error while running javap B.
    While "javap -private B". is working fine.
    #-Link-Snipped-# , can you confirm this.
    show me the error and your B.java ?
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberJun 21, 2012

    sookie
    show me the error and your B.java ?
    😲, its working, what's wrong , I was doing

    Here is final conclusion
     
     
    With public constructor
    Source:
    public class abc{
       
        abc(){}   
        public static void main(String[] args) {
            System.out.println("Hello, World");
        }
     
    }
    Output
     
    C:\Documents and Settings\<user>\My Documents\Downloads>javap  abc
    Compiled from "abc.java"
    public class abc extends java.lang.Object{
        public abc();
        public static void main(java.lang.String[]);
    }
     
    With private constructor
    Source:
    public class abc{
       
        private abc(){}   
        public static void main(String[] args) {
            System.out.println("Hello, World");
        }
     
    }
    Output
     
    C:\Documents and Settings\<user>\My Documents\Downloads>javap  abc
    Compiled from "abc.java"
    public class abc extends java.lang.Object{
        public static void main(java.lang.String[]);
    }
     
    With private constructor
    C:\Documents and Settings\<user>\My Documents\Downloads>javap -private abc
    Compiled from "abc.java"
    public class abc extends java.lang.Object{
        private abc();
        public static void main(java.lang.String[]);
    }
    Are you sure? This action cannot be undone.
    Cancel
  • sookie

    MemberJun 21, 2012

    ianoop
    😲, its working, what's wrong , I was doing
    Cool...😀
    Are you sure? This action cannot be undone.
    Cancel
  • Vikash Verma

    MemberJun 24, 2012

    #-Link-Snipped-# you are right...😎
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register