• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessibility of default constructors

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Ria,
think inner classes!!!
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
reply
    Bookmark Topic Watch Topic
  • New Topic