This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessibility of default constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Accessibility of default constructors" Watch "Accessibility of default constructors" New topic
Author

Accessibility of default constructors

Ria Mathur
Greenhorn

Joined: Oct 11, 2001
Posts: 14
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


-Ria
J Hreich
Ranch Hand

Joined: Mar 22, 2002
Posts: 37
Hey Ria,
think inner classes!!!
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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 ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

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

Joined: Mar 11, 2002
Posts: 143
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

Joined: Aug 26, 2001
Posts: 7610
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

Joined: Sep 22, 2001
Posts: 2578

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessibility of default constructors
 
Similar Threads
Overloading constructors.
Default Constructor
Implicitly given access modifier
Object Class
Access control