• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

naming : package, inner classes

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
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
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.
 
See El
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
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
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
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
Yes, with
p1.i newobj = new p1.i()
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Valentin:
Following code does not compile:

Compiler cannot resolve p1.i and p1.i()
 
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
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
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes.
I am runing 1.3 ver of JDK on windows 98.
 
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
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
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the answer for the other forum :

and it works !!!
Still don't know how to declare an object though.
 
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
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.
 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic