Forums Register Login
Accessibility of default constructors
The statement is from Manish's notes -
"If the compiler gives you the default no-args constructor for the class, its accessibility is same as the class. "
How can we see accessibility of the constructor which is given by the compiler? I mean since it is not in the source file?
- Ria
Hey Ria,
think inner classes!!!
You can't see it in the source file but you can see it in the class file.
Compile this:

And then decompile it using javap:
javap -c -l -s Test1
You are going to get this:

As you can see the default no-arg constructor is public.
Now do the same with the following code:

Decompile it and you'll see this:

The constructor has a default accessibility just as the class Test2.
This confirms Manish's notes.
Learn how to use javap, you'd be amazed what you could discover with it.
[ March 26, 2002: Message edited by: Valentin Crettaz ]
My first approach was to use reflection, but reflection will only find you the public constrcutors. You can, do this:

This solution isn't quite what you'd like, I'm sure. If you really want to see the inserted constructor, you'll have to look at the generated byte-codes. Look at Val's example above.
Thanks Valentin, I was going to suggest javap only
It really gives you an interesting insight into java classes.
- Manish
Val,Corey,Manish thanks I have learnt something new here by the question posted by Ria -> Thank U .
Valentine decompiling with javap-c-|-s Test1 . Should this command be from the DOS command prompt. Is it possible from textpad. I am using Textpad & dos prompt for compiling and running codes. Is there anything better out there for a beginner like me.
javap is part of the J2SDK suite tools. it is located in $JAVA_HOME/bin and is a normal utility just like java and javac.
Moreover, TextPad and a DOS console is the best you can hope for as a beginner. That's what I would recommend to any beginner: Your favorite text editor, the J2SE and a terminal. That way you really get a good idea of how Java works. When using IDEs all the dirty work is taken care of by the tool for you and you never know how that compilation/execution stuff works. Way to go
Besides, it is possible to configure your Textpad so that you can see the result of compiling, execution etc in the textpad window itself. That way you have all your learning, and you don't have to switch to dos prompt every now and then. I am sure that you would be able to configure textpad to run Javap as well.
- Manish
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards

This thread has been viewed 729 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jun 21, 2018 21:35:28.