• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner classes  RSS feed

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,have seen this code in one of the forum.



when i execute the above program i get an error saying"possible loss of precision".
when i correct the statement "return 1.3 to return 1.3f" it runs fine.
now my question is Myinner is a private class,so you cannot access it directly from outside code, for eg, new Myouter().new Myinner().f(); so it should give a compile error,but when i run the code it is not giving any error.what might be the reason?

 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac 6 compiler says this to me:

Outside.java:23: f() in kethysiera.innerclass.Myouter1.Myinner is defined in an
inaccessible class or interface
System.out.println(outer.getIt().f());
^
1 error



 
Vinu Menon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mine is jdk 1-5
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my jdk1.5 is also saying compile time error:


F:\workspace\scjp\src>javac -version
javac 1.5.0_05
.....

F:\workspace\scjp\src>javac kethysiera/innerclass/Outside.java
kethysiera/innerclass/Outside.java:23: f() in kethysiera.innerclass.Myouter1.Myi
nner is defined in an inaccessible class or interface
System.out.println(outer.getIt().f());
^
1 error




 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does the error REALLY depends on version....
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

James Tharakan wrote:does the error REALLY depends on version....



For some API, like SortedSet, SortedMap those are added from java 6 and generics, StringBuilder added in java 5. I donot think this error will depend on java version.

There is something wrong. Vinu show your command line compiling version and compiling outputs as well as running output.
 
Vinu Menon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

D:\scjp>javac Outside.java

D:\scjp>java Outside
1.3
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Vinu Menon wrote:
D:\scjp>javac Outside.java

D:\scjp>java Outside
1.3



show the output of these commands as well:

1) javac -version
2) java -version
 
Vinu Menon
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D:\scjp>javac -version
javac 1.5.0
javac: no source files
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files
-cp <path> Specify where to find user class files
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-endorseddirs <dirs> Override location of endorsed standards path
-d <directory> Specify where to place generated class files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release

-target <release> Generate class files for specific VM version
-version Version information
-help Print a synopsis of standard options
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system


D:\scjp>java -version
java version "1.6.0_11"
Java(TM) SE Runtime Environment (build 1.6.0_11-b03)
Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing)
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I really do not know what is the problem ?
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I don't know either. Here's my java -version output:
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

I modified the code a little bit to illustrate something interesting:

Output is:
In Myouter.Myinner
 
Java Cowboy
Sheriff
Posts: 16084
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags when you post source code.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jesper Young wrote:Please use code tags when you post source code.


Hi Jesper,

Are you seeing my code out of code tags, or are you referring to some other post? I want to make sure you are not seeing something that I am not because of a different browser or something.

Thanks.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ruben Soto wrote:

Jesper Young wrote:Please use code tags when you post source code.


Hi Jesper,

Are you seeing my code out of code tags, or are you referring to some other post? I want to make sure you are not seeing something that I am not because of a different browser or something.

Thanks.



Jesper has added code tag to Vinu's post, so he is saying to Vinu.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruben what do you want to show in your example? And what about above example, is that running in your complier?
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Punit Singh wrote:

Ruben Soto wrote:

Jesper Young wrote:Please use code tags when you post source code.


Hi Jesper,

Are you seeing my code out of code tags, or are you referring to some other post? I want to make sure you are not seeing something that I am not because of a different browser or something.

Thanks.



Jesper has added code tag to Vinu's post, so he is saying to Vinu.


I see. I didn't realize he had added the tags to Vinu's post.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Punit Singh wrote:Ruben what do you want to show in your example? And what about above example, is that running in your complier?


Hi Punit,

The original example doesn't run for me either, I have the same issue as you do.

The example that I showed I think is a little interesting, because no matter if the original class is not visible, you can obtain an object of that class, but you are forced to treat it as an Object instance. If however you override any of the Object methods in the invisible class, polymorphism is still in effect. I thought it was interesting.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then what are you getting output for this line:
System.out.println(((Object)outer.getIt()).toString());

I am getting:

kethysiera.innerclass.Myouter$Myinner@addbf1



But as per your explanation I was expecting:

"In Myouter.Myinner"


 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Punit Singh wrote:Then what are you getting output for this line:
System.out.println(((Object)outer.getIt()).toString());

I am getting:

kethysiera.innerclass.Myouter$Myinner@addbf1



But as per your explanation I was expecting:

"In Myouter.Myinner"



I am getting "In Myouter.Myinner"

I think this must be another mystery like what happened with your classpath

Punit, try to just copy and paste my example in notepad, then run it from the command line to see what you get.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes
From command line I am getting "In Myouter.Myinner". Wow a new mystery.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know. How are you running this when you get the other result?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ruben Soto wrote:I know. How are you running this when you get the other result?



Actually that was my fault, I had two class one in MyOuter, and second is MyOuter2, I had overriden toString() in MyOuter2 class, but used MyOuter class for test, now it is ok. Eclipse is also giving the same output.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Even i got " javaapplication1.Myouter$Myinner@addbf1" as output.

When observed, i realised that
i only modified this part
System.out.println(((Object)outer.getIt()).toString());

but forgot to add the tostring() which was not there in the original code
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Case closed.
 
a fool thinks himself to be wise, but a wise man knows himself to be a fool - shakespeare. foolish tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!