Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessibility of default constructors

 
Ria Mathur
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks,
- Ria
 
J Hreich
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ria,
think inner classes!!!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Corey
 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Valentin, I was going to suggest javap only
It really gives you an interesting insight into java classes.
HTH,
- Manish
 
Arun Pai
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manish Hatwalne
Ranch Hand
Posts: 2595
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
HTH,
- Manish
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic