This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Static vs. Public: Understanding the differences  RSS feed

 
Matt Hazan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I was wondering if someone could explain the main differences between static and public in a way that it easy to understand, for both, variables, and methods.

Sometimes when I code I get an warning similar to "this method should be implemented in a static way", I don't really understand why I get this error in the first place.

I know that static variables cannot be accessed by anything outside the package and public variables are free to be accessed/ and modified by anyone inside or outside the package.


Really, what I am looking for in this thread is the simplifaction of static vs public variables/ methods, if anyone could help me understand this, I would greatly appreciate it.




 
Mohamed Sanaulla
Bartender
Posts: 3179
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static has got nothing to do with the visibility/scope of fields or methods. Its just that these dont require an instance of the class and can be accessed as ClassName.fieldName or ClassName.methodName. The warnings you see are might be due to the fact that the method doesnt use any of the instance fields and might be not dependent on availability of any instance. But I think you might have seen these warnings in the IDE?

And public is used to define the fields/methods visibility. Public is the most liberal visibility and the fields/methods declared can be accessed anywhere in the code. Then you have protected, private and the default which is pacakge scope.
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'static' means the variable or method is defined for the class itself; this is in contrast to an instance variable, which is defined for objects that are instances of the class.

Therefore, if you have a static variable, then no matter how many objects you have that are instances of that class, you still only have one variable by that name. You have a different set of instance variables for each object, but only one of each static variable.

If I have a Car class, and it has a static int variable carCount, and I instantiate 100 Car objects, I still have only one carCount variable, and I could keep a count of each car I instantiate there if I want.

A static method can be invoked without using an instance of the object. Let's say I have a method that returns the count of cars, using the static variable carCount, called getCarCount(). It would not make as much sense to define this on an instance of Car; it is not related to any one Car, but to all the Car objects. So getCarCount() could be a static method, and could be invoked with "Car.getCarCount();". If you instantiate Car and use the instance variable familyCar to hold a reference to it, then using familyCar.getCarCount() will give you the message you speak of, since you are invoking a static variable but using an instance to do it.

'public' just means that any class can access the variable or method. this is in contrast to 'private' that limits access to code within the class, and a couple of others that limit it other ways.

Is that what you were looking for?

rc

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