• Post Reply Bookmark Topic Watch Topic
  • New Topic

need clarification on 2 errors from a Java book I'm reading  RSS feed

 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd appreciate a clarification on the code below describing why the 2 lines marked "//error!" are incorrect (especially the 2nd //error comment):

public class Outer {
public int x;

public class InnerOne {
public int fn() {return x;}
}

public class InnerTube {
public int fn()
{return x;} //error!
}
}

public class OuterTest {
public void test() {
new Outer.InnerOne(); //error!
new Outer.InnerTube();
}
}


One last question: is there a difference between a "static" method and a "final" method?

As always, thanks to all who reply!
Dan
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not an error. It compiles fine.

Both lines in OuterTest.test() methods are errors. You can't create instance of inner class withour referring to outer class. you should have used this instead:
Here you are creating an Outer instance and then InnerOne using newly created Outer instance.
Or alternatively:


Static method is a method that belongs to a class rather that an instance.
Final method is a method that can't be overriden.
 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel,

The code shown was a direct copy from the O'Reilly book, "Programming Android"/p.45 so for now I'm inclined to believe the authors (whether it compiles or not).

But thanks for your reply,
Dan
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused. You post some code that has errors in it and ask for help. Pawel tells you what's wrong with the code and you reject that reply because it's different to what's in the book.
If we're not allowed to suggest changes to the code to get it to compile, what are you expecting from us ?
 
Charles D. Ward
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class Outer compiles fine. The 2 erros are in the OuterTest class and the compiler tells you exactly what's wrong:



Pawel already answered how to fix it.
 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne,
Charles,

So what you're saying is that the book is wrong?
Though that's certainly possible the narrative that discussed this example suggested otherwise, which is why I posed this question to the forum.
I chose not to influence any responders with what was published so let me just close this issue and rethink their (and your) discussion.

Thanks for your reply,
Dan
 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne,

BTW, perhaps I wasn't clear. I was not looking to change the code so it would compile but rather to understand why the authors considered those errors as violating Java rules (which I am still unclear on).

Dan
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Bromberg wrote:Pawel,
The code shown was a direct copy from the O'Reilly book, "Programming Android"/p.45 so for now I'm inclined to believe the authors (whether it compiles or not).

You must have a strange understanding of direct copy ;).
That is a direct copy:


The difference is in line 8.
You wrote:
It should be:

The static keyword really makes a difference!
You can think of it as:
  • static thing doesn't need its own instance, and
  • static thing can't have access to instance (non-static) things (variables, methods)


  • Then, let's analyse this code:

    Line 10 does not compile because non-static fields (like x) can not be referenced from a static context.
    Remember, a static thing (class InnerTube) can't have access to instance things (variable x).

    Line 17 des not compile because InnerOne is not a static class, so it needs an instance of Outer. I explained it before.
    Line 18 compiles because InnerTube is static. And, as I said, static thing (class InnerTube) doesn't need its own instance (of outer class Outer).
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I understand the confusion now. It would have helped if you'd made it a bit clearer that the code was not meant to compile - that it was an example from a book that was deliberately showing incorrect code.
    Anyway, hopefully Pawel has cleared up your doubts now.
     
    Dan Bromberg
    Ranch Hand
    Posts: 52
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Pawel,
    Apologies, apologies, apologies! I cannot believe that I omitted the "static" keyword and am truly sorry if I led you on a wild goose chase. Since I did not have access to an electronic version of the code (not sure where you found it (is there an e-version of this book available?)), I had to hand type it and obviously made a clumsy mistake in transcribing.
    Your explanation was very clear and I thank you for your patience.
    Dan
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!