• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static question  RSS feed

 
Glenn Jayasuriya
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I get a "non-static variable this cannot be referenced from a static context" error if I have this line: class Student.
But if I have: static class Student, it compiles and runs.

Here is my code:


Why does this happen and is it proper to just add a static before class Student?
The only thing I know about static is that if you have multiple objects, static variables will be shared/updated for each instance if a change occurs.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When we're talking about classes, a static inner class (only inner classes can be static) is one that is not associated with an instance of the outer class. In this case, when you declare Student as a static class, it does not associate with any particular instance of the objectPrac class. When declared non-static, you must first create an instance of the outer class to make a new Student.

The syntax for creating a new non-static inner class instance is quite odd. Here's how you do it:


In this case, it looks like a better solution would be moving the Student class outside of objectPrac completely. But if your assignment makes you put it in, then it sounds like static is the correct choice.

(Also: static variables are completely different than static classes. Don't confuse the two.)
 
Tim Cooke
Marshal
Posts: 4039
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zachary Griggs wrote:The syntax for creating a new non-static inner class instance is quite odd. Here's how you do it:


I had not seen this syntax before so today I have learned something about the Java Language. Nice!

However, I sure hope I never see this syntax ever again. Yuck, Yuck, Yuck! with a capital Oh My Eyes!!

Moving the inner Student class out to its own class file would be my preferred approach here.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that's why any decent class makes its inner classes private and gives a nicer way of making them (iterators, for example)

I'm not sure why the syntax is like that, but I can take an educated guess. Imagine that we were working inside an instance of the outer class, so we have a "this" reference. The syntax would be:

Since this is implied, we can remove it:

And it looks just like your typical instantiation. I don't think you were ever supposed to do this externally!
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:However, I sure hope I never see this syntax ever again. Yuck, Yuck, Yuck! with a capital Oh My Eyes!!


Try this syntax for eye-wateringness then:

 
Bill foster
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from the tutorial on inner and outer classes:



I'm confused because the public static void main( ) would be in the testing class (to test the objects and methods of that class). In the tutorial the inner class has no static in it.
I bet if you write the code as the tutorial says that there would be no need to use static. My question is why is this so?

Bill
 
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
Bill foster wrote:I'm confused because the public static void main( ) would be in the testing class (to test the objects and methods of that class). In the tutorial the inner class has no static in it.
I bet if you write the code as the tutorial says that there would be no need to use static. My question is why is this so?


Not sure of the concern here. Static methods can access non-static methods. It just needs a reference to do it. And this is true with top level classes too (not just related to inner classes).

Henry
 
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
Tim Cooke wrote:
I had not seen this syntax before so today I have learned something about the Java Language. Nice!

However, I sure hope I never see this syntax ever again. Yuck, Yuck, Yuck! with a capital Oh My Eyes!!

Moving the inner Student class out to its own class file would be my preferred approach here.


If you like that syntax, then you are going to love it the syntax when one inner class derives from another inner class, that is in different outer class. In order to instantiate that subclass, you would need two outer classes -- one for the subclass, and one for the superclass...

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!