• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner class  RSS feed

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code works fine when I use makeInner method,But without, does not compile. Why?
 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This has nothing to do with the class being inner or not.
Those statements (lines 6-8) can't be placed directly in a class' body.

I am not sure what do you mean by use makeInner.
Do you mean enclosing those statements (lines 6-8) within a method?
Or maybe you meant calling the method in line 15?
 
Marshal
Posts: 56610
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I see you are one of the people who use i = i++; and think it actually does something.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And I see you are one of the people who use i = i++; and think it actually does something.

I didn't notice that.

This is so common that we have a FAQ entry about it.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote: . . . I didn't notice that. . . ..
That is why you ask questions on a forum. If one person doesn't notice something, there is a good chance somebody else will.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for answer


This code doesn't compile and throw exception.Why?

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another code:


This code throws this exception. why?
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class MyInner
location: class myouter2.MyOuter2
at myouter2.MyOuter2.main(MyOuter2.java:34)
Java Result: 1


 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abalfazl hossein wrote:Another code:

This code throws this exception. why?
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
symbol: class MyInner
location: class myouter2.MyOuter2
at myouter2.MyOuter2.main(MyOuter2.java:34)
Java Result: 1




As already mentioned... please read this again...

http://www.coderanch.com/how-to/java/FixAllCompilerErrorsBeforeRunningTheApplication


Then, show us the first few errors from the compiler. And not from the IDE that is running the application.

Henry
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try the indenting convention we have here with every { and every } on a line by itself. See whether that explains your problem.
 
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And I see you are one of the people who use i = i++; and think it actually does something.

FWIW, I never even use i++ to add one to a variable (with the exception of for-loop counters). I always write i = i + 1, on the belief that it more explicitly reflects what I'm doing. (Haven't looked in years, but as long ago as 1984, the C compilers I used would compile this into the same code as i++ generated, so it made no actual difference of any kind, at run time.)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:
Campbell Ritchie wrote:And I see you are one of the people who use i = i++; and think it actually does something.

FWIW, I never even use i++ to add one to a variable (with the exception of for-loop counters). I always write i = i + 1, on the belief that it more explicitly reflects what I'm doing. (Haven't looked in years, but as long ago as 1984, the C compilers I used would compile this into the same code as i++ generated, so it made no actual difference of any kind, at run time.)


I believe, if memory serves, with the latest version of ANSI C -- it will do pretty much the same thing as it is done in Java, in that the end result of i will remain unchanged. With older version of C, the specification was actually not that clear -- and as such, some C compilers will increment the variable, while others will not (dependent on when the actual post increment will occur).

Henry
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
… lo and behold, as you said, the two are the same.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, they aren't. I should have looked properly.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:No, they aren't. I should have looked properly.

Yeah, you're right. I never looked at compiled Java before, only C (and a long time ago). Far as I can tell (and I ain't no bytecode expert), aload_0 and dup should take the same time to execute, so it shouldn't matter which form you use.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this line has problem in this propgram: MyOuter2.MyInner inner = new MyOuter2().new MyInner();

 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote:
Campbell Ritchie wrote:No, they aren't. I should have looked properly.

Yeah, you're right. I never looked at compiled Java before, only C (and a long time ago). Far as I can tell (and I ain't no bytecode expert), aload_0 and dup should take the same time to execute, so it shouldn't matter which form you use.

Sorry if this is not what the OP wanted to discuss, but I realized I made a mistake earlier when I said I use i = i + 1 instead of i++. I just wrote a line of code a moment ago and that line was this:
and that is what I use instead of i++. So, correcting for my dumb mistake failing memory, here's Campbell's sample, recoded to use what I really do, and the resulting bytecode:


Those do appear to be the same code. (But, can anyone tell me what Line 26 means?)
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SM: Line 26 is a continuation of line 25 which was too long. It means (I think) that the (notional) <init> method of java.lang.Object takes no arguments: empty ().

And I think aload0 twice has exactly the same effect as aload0 dup, so that difference is only notional.

AH: Please post the exact compiler error. If you don't get a compiler error stop using IDEs and use the javac tool directly. Or look at what comes up when you hover your mouse over the red mark against that line.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:SM: Line 26 is a continuation of line 25 which was too long.

Doh!

Thanks
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stevens Miller wrote: . . . Doh!

Thanks
You're welcome but it is not a case of D'oh (with apostrophe ‍). The ()V syntax is not obvious unless you have seen it before, so I did not expect everybody to understand it. I remember reading it in a book somwhere, but can't remember where.
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Stevens Miller wrote: . . . Doh!

Thanks
You're welcome but it is not a case of D'oh (with apostrophe ‍). The ()V syntax is not obvious unless you have seen it before, so I did not expect everybody to understand it. I remember reading it in a book somwhere, but can't remember where.

I believe more than one form is acceptable. (It's not that it should have been obvious what it meant. It should have been obvious to me that it word-wrapped. I was trying to figure out what it meant as a line all its own. )
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful, or I shall have to move you to MD
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!