• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static in local class, some confusion  RSS feed

 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit: I decided to post this on stack over flow instead. I'll let it as because it might be interresting for others

I've been brushing off my java and I've some misunderstanding about local classes (that I ultimately never used), I well understand the concept of static but not in the case of local classes.
1. Why is a static method not allowed in a local classes:
2. Why is a static local class not allowed in a method ?


1. A static method not allowed in a local classes:

Here I don't get it. To me the local class is tied to the static method main. I just don't understand why this cannot be done. The method main is accessed through the Sequence class and then since sayGoodbye is static it should be accessed through its class. But no.


2. A static local class not allowed in a method :

This cannot be done: It's a bit ambiguous but I'd think a static here would have the same meaning as a non static since the static class is tied to a static method. I'm confused.


 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say "local class", do you mean an inner class? A class inside another class? Let's assume so.

You can have a static member in an inner class if the inner class is static. If you ask why, I would say, How would you access the static member otherwise?

Secondly, you can't have classes of any kind inside methods. (You can't have methods inside methods either). If you ask why, I would say... well, again, how would you access them? And why would you need them? You can use an inner class declared outside the method from within a method, so just do that.

There are others who can answer the "why" much better than I can.
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:When you say "local class", do you mean an inner class? A class inside another class?


No I mean local class. I understand inner class well.

Secondly, you can't have classes of any kind inside methods.


Yes you can, it's called a local class. You just don't know because it's never used and arguably, and that's purely my opinion, bad practice because people will twitch when seeing it for the first time. Are you an admin by the way ? Because if even an user with 2026 posts can't answer it (No offence, I didn't know about it yesterday either) I think this post belong in the JAVA in general sub..

More info https://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html

Local classes are classes that are defined in a block. You typically find local classes defined in the body of a method.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cedric Bosch wrote:No offence

No need to be rude.

Cedric Bosch wrote:Why is a static method not allowed in a local classes

The static keyword is a special keyword that says "I am a member of a top-level class". A local class is not a top-level class and so it doesn't have the privilege of having such a member; however, the makers of Java granted it the special privilege of having a final static field. Only a static nested class has the same status as a top-level class. Every other class has a lower status with limited privileges.

Cedric Bosch wrote:Why is a static local class not allowed in a method

Again, the static keyword is a special keyword that says "I am a member of a top-level class". Anything declared inside a method is not a member of anything.
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The static keyword is a special keyword that says "I am a member of a top-level class".

Fair enough. Still they could have designed it so static means either member of a class or block depending where it's placed. They didn't do that and that's inherently what my question is about.

As I said, I posted it on stackoverflow. ( I kinda wanted too post it on stackoverflow because when posting here I figured it would get me points there... and it's my most voted question only after 12 hours. My e-dick is arroused) so sorry for cross posting

Here is the answer : http://stackoverflow.com/a/37635791/4299560

The gist of it is that there is no real value of having this functionality.

I still like your explannation a lot because it's easier to remember.

Joe Bishara wrote:
No need to be rude.


I think you are kidding but just in case : I thought this guy might be one of the most experienced here, so I figured if he couldn't answer others probably wouldn't. I just rated him highly because of his posts count. I didn't mean yo be rude
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I actually use local classes a lot for writing custom accumulation types for collection operations.

As has been mentioned, making local classes static makes no sense. They belong to a method, and not to a class.

Members of a local class could have been static, meaning they can be accessed without needing an instance of the local class. The designers probably decided that this use case is so rare that it's not worth the trouble to implement it. After all, static members of a local class are only accessible to the method that contains the class, so you might just as easily declare them as private static members of the top level class.
 
Liutauras Vilda
Marshal
Posts: 4660
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cedric Bosch wrote:I thought this guy might be one of the most experienced here, so I figured if he couldn't answer others probably wouldn't. I just rated him highly because of his posts count.
You were right in your initial thinking, Knute Snortum is one of the most experienced. Nevertheless, nobody can know everything, there are too many things out there. That is why such communities and forums exist, which provide you a framework to share your own knowledge with less experienced and at the same time lets you learn from more experienced guys or from the ones who have expertise in particular area of field.
 
Cedric Bosch
Ranch Hand
Posts: 99
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:You were right in your initial thinking, Knute Snortum is one of the most experienced. Nevertheless, nobody can know everything, there are too many things out there. That is why such communities and forums exist, which provide you a framework to share your own knowledge with less experienced and at the same time lets you learn from more experienced guys or from the ones who have expertise in particular area of field.


Agreed. I didn't mean to belittle his knowledge in the contrary.

Stephan van Hulst wrote:
Members of a local class could have been static, meaning they can be accessed without needing an instance of the local class. The designers probably decided that this use case is so rare that it's not worth the trouble to implement it.


I think that's what it boils down to, or maybe to not introduce any confusion on the meaning of static.

Stephan van Hulst wrote:After all, static members of a local class are only accessible to the method that contains the class, so you might just as easily declare them as private static members of the top level class.


I disagree with that though but maybe I'm wrong. Principle of least privilege, if the private method was member of the top level class it could be accessed by other methods. What I think is a better explanation is:

vs

If they designed it like that it would save one line of code on an infrequent scenario. Thus not really worth it. Maybe I'm wrong but that makes more sens to me

I know I'm being pedantic for something I'll probably never use lol, it just irks me a lil bit.

cheers and sorry knute
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!