• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Frits Walraven
  • Himai Minh

Static vs. Public: Understanding the differences

Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Posts: 3225
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?


You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic