• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Variables - Instance versus Class/Static

 
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain to me the difference between an Instance variable and a Class/Static variable?

You can only have one Class variable; what good would having only one do for solving your business problem with the constraint of only one variable?

Thanks.



 
Bartender
Posts: 7197
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Royale Summers
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey -

From the YouTube video I'm currently watching: https://www.youtube.com/watch?v=hBh_CC5y8-s

He says, "...one per class" at the 1:09:00  mark.

Please let me know if you have any additional questions.  Thanks!
 
Carey Brown
Bartender
Posts: 7197
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah.

With an instance (non-static) variable, there is a new one for every instance created of that class. If you have not created any instances, then no storage for that variable is available. You can define as many instance variables (aka member variables, aka fields) as you'd like.

Static (Class or non-instance) variables (also includes constants) have storage allocated for them from the time the class is loaded into the JVM, that is, when the class is first encountered. There is only one block of memory allocated to static variables regardless of how many instances of the class have been made (including zero). A static variable is shared among all methods that have access scope. You can have as many static variables as you'd like.
 
Royale Summers
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey -

Thanks.  I'm going to have to re-read your response a couple of more times for it to sink in.

I found this link that provides an exercise: https://www.guru99.com/java-static-variable-methods.html#1

If I'm understanding then, these Class variables would be Constants?
 
Marshal
Posts: 25677
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Royale Summers wrote:If I'm understanding then, these Class variables would be Constants?



I'm not sure why you think that. My guess: that tutorial said they are initialized only once, when the class is loaded. That much is true, but it doesn't say that they can't be assigned different values later.
 
Royale Summers
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul -

I'm not understanding what's their purpose; so, that's why my brain conjectured that...
 
Paul Clapham
Marshal
Posts: 25677
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's true... when you try to learn about abstract concepts which you don't understand, it's natural for statements about those concepts to be equally (or more) hard to understand.

It would make sense to learn about objects first, and then to learn about their features. It seems that your understanding of objects isn't up to that level yet. But it's very common for beginning programmers to not understand objects right away, especially (I think) if they've already spent a lot of time learning programming without objects.

It's also difficult to learn about abstract concepts in general, for most people anyway. For me it makes it easier if I have concrete examples which illustrate the abstract concepts.
 
Carey Brown
Bartender
Posts: 7197
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For "a given" static variable, you only ever have one for the class. This is true of each static variable, and you can have as many as you want. Also, no matter how many instances of a class you create there will still be only one each of the declared static variables.

any better?
 
Sheriff
Posts: 15784
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Examples will probably be best to help you understand.

A lot of it has to do with semantics, particular the semantics around “whose value is this?”

Say you had a class Dog. Each instance of Dog will have attributes particular to that instance. Say weight, date of birth, name, whether or not it’s housebroken, These would be represented as instance variables. However, there are values that are shared by the entire class of Dog, like scientific name (Canis lupus familiaris). It doesn’t make sense to have every Dog object have “its own” copy of scientific name because it never varies from one Dog to the next. Therefore, scientific name makes more sense as a class variable for Dog, represented as a static variable.

In code, that would look something like this:

On line 2, public says “This is publicly available/accessible,” static says “This is shared by all Dog instances,” and final says “This never changes / is a constant.”
 
Junilu Lacar
Sheriff
Posts: 15784
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Royale Summers wrote:I found this link that provides an exercise: https://www.guru99.com/java-static-variable-methods.html#1


That’s a poor quality site both in content and form. I would steer clear of that site if I were you.

I’d also be very careful about which videos on YouTube to watch. It’s very prudent to verify any information you get from those against authoritative sources like the JLS, the official Java tutorials and API documentation, and (all modesty aside) the folks here.
 
Marshal
Posts: 69809
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . static says “This is shared by all Dog instances,” . . .

It also means there is one copy of that variable, irrespective of the number of instances. There is one variable if there is 1 instance, 2 instances, 3 instances, 1000000000 instances or even no instances.

[edit]I see Carey has already said that.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul-

Thanks for your input but I do understand Objects.

They model the  real-world and are considered Entities that have: State, Behavior, and Indentity.

Junili -

Thanks, I understand your description but the example was a plus to solidify my understanding.

So, there's only one and it is usually a Constant?

Thanks for the assistance of everyone!
 
Campbell Ritchie
Marshal
Posts: 69809
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Royal Summers wrote:. . . So, there's only one and it is usually a Constant?

Yes, and no. There is only one of each static field, but it doesn't have to be a constant. Some people think there should be no such thing as a static variable using the term in its strict sense as meaning something that might change. They would make all their static fields constants.

Thanks for the assistance of everyone!

That's a pleasure
 
Ranch Hand
Posts: 84
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Royale Summers wrote:Can anyone explain to me the difference between an Instance variable and a Class/Static variable

every body has explained well,
I want to say that static variables global to all the objects of class, you can update these variable any where from the class and it stores new value every time you update it, but instance variable are not updated for each object. Mean every creation of object cantain its own instance variable
 
Greenhorn
Posts: 7
Eclipse IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This concept was somewhat difficult for me initially to get my head around.  What helped me most in my understanding of "static" is this -

Something that is static always "belongs" to a class, not the object.  Although Objects can access and modify static,  static can never refer to any one Object.

static variable - belongs to the class
static method - depends on the class (type) that references it
static initializer - run once only when the class is first used

This might be 'technically' wrong, but works for me.
 
Campbell Ritchie
Marshal
Posts: 69809
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Billy Willis wrote:. . . This might be 'technically' wrong . . .

Slightly odd nomenclature maybe, but it looks more like ’technically‘ right to me
Static initialisers run when the class is loaded, which might be before it is actually used.
 
Let nothing stop you! Not even this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic