my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes naming : package, inner classes 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 "naming : package, inner classes" Watch "naming : package, inner classes" New topic
Author

naming : package, inner classes

See El
Ranch Hand

Joined: Oct 07, 2002
Posts: 44
Hi everybody,
I've got an question regarding the following code :

The question is what does it print out ?
.....
The answer is :
inner
inner

Now the next question is how to declare & create the ("outer") "i" class within class Class1 ?
Thanx in advace,


See El<br />SCJP2, SCWCD, IBM XML, SCBCD
smita raval
Greenhorn

Joined: Aug 31, 2002
Posts: 28
It's really an intelligent question. From where did you get it ?
(Anyway, still I couldn't find the way to declare and use outer i in class Class1.) :roll:


Smita Raval
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
From JLS 6.3.1 Shadowing Declarations

A declaration d of a type named n shadows the declarations of any other types named n that are in scope at the point where d occurs throughout the scope of d.

In clear, the declaration of the static class i in Class1 shadows the declaration of class i in package Class1.


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

Joined: Oct 07, 2002
Posts: 44
I'm preparing for SCJP and just made this up to test the package naming tricks.
The 2-nd question remains open ? Anynone ?
Is it possible or it is not ?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
2nd Question:
I do not think you can do that unless you change the name of the outer class. Because you have same names, the inner static class shadows the outer i class.
Even if you change the name of the package to say p1 and execute following statement:
p1.i i2 = new p1.i();
it gives compiler error because you can not reference p1 from within p1.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
From JLS 6.5.2 Reclassification of Contextually Ambiguous Names

If the AmbiguousName is a qualified name, consisting of a name, a ".", and an Identifier, then the name to the left of the "." is first reclassified, for it is itself an AmbiguousName.
There is then a choice:
If the name to the left of the "." is reclassified as a PackageName, then if there is a package whose name is the name to the left of the "." and that package contains a declaration of a type whose name is the same as the Identifier, then this AmbiguousName is reclassified as a TypeName. Otherwise, this AmbiguousName is reclassified as a PackageName. A later step determines whether or not a package of that name actually exists.

Package Class1 contains a type declaration called Class1 and Class1 is then reclassified as being the type name instead of the package name. There is no chance you can instantiate the top-level class i.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Posted by Valentin:

Package Class1 contains a type declaration called Class1 and Class1 is then reclassified as being the type name instead of the package name. There is no chance you can instantiate the top-level class i.

If package name is changed to p1, is there anyway an instance of top level class i can be created within main() of class1....
[ October 07, 2002: Message edited by: Barkat Mardhani ]
[ October 07, 2002: Message edited by: Barkat Mardhani ]
[ October 07, 2002: Message edited by: Barkat Mardhani ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Yes, with
p1.i newobj = new p1.i()
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Valentin:
Following code does not compile:

Compiler cannot resolve p1.i and p1.i()
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
It compiles fine on my machine... Are you sure you have the code in a file called Class1.java in a directory called p1?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Yes.
I am runing 1.3 ver of JDK on windows 98.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Here is my code:

I'm in directory p1.
Compile with: javac Class1.java
Execute with: java -classpath .. p1.Class1
Output:
inner
inner
outer
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Thanks Valentin for you help. After some play with the code, if I found that there some other files in p1 package with class i in them. That caused the grief. I created a brand new package and it worked fine....
See El
Ranch Hand

Joined: Oct 07, 2002
Posts: 44
I got the answer for the other forum :

and it works !!!
Still don't know how to declare an object though.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Very interesting...
But the problem is now that newInstance() returns an Object that has to be cast to be correct type in order to be useful. I think there is no way to cast the created object to class i since either casting to i or Class1.i will resolve to the static class declared within the class Class1 and not to the top-level class i declared at the top of the compilation unit.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: naming : package, inner classes
 
Similar Threads
Inner Class - Initialization Sequence
confused question
Accessing component from main class.... Help!
Confusion Question
Accessing outer class members from instance of inner class