• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getter for static variable  RSS feed

 
Chris Yates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you write a getter for a variable that is static say static int should the method return just int or since it's static variable it should return static int? Or no difference?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should return "int", but the method also should be marked as static. When you see a method declared as "static int", only the "int" part is the return type. The "static" means that you don't need to create an instance of the class to call the method.
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does "return static int" mean?
 
Chris Yates
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:It should return "int", but the method also should be marked as static. When you see a method declared as "static int", only the "int" part is the return type. The "static" means that you don't need to create an instance of the class to call the method.


Oh i see. But then why is it not common practice to declare all getters static?
 
Knute Snortum
Sheriff
Posts: 4081
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Getters don't usually return static variables. If you have an object you want to return the variable of that object. A static getter could only return a static variable.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some people say there should be no such thing as a static variable at all in an object language.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Some people say there should be no such thing as a static variable at all in an object language.

Hear, hear.

@Chris: The problem is not with a static getter; it's with the idea of a static variable - which suggests a "setter" as well.

My advice: Don't do it.

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