Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8292089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:21:07+00:00 2026-06-08T13:21:07+00:00

Here is a generic class that I have defined, what I would like to

  • 0

Here is a generic class that I have defined, what I would like to know is when I am creating more specific classes for instance a CAR class when would I use a Class Variable? My personal understanding of a class variable is that a single copy of a class variable that has been declared in a class will be declared using the keyword static, and that each object that has been instantiated from the class will contain a single copy of the class variable.

An instance variable allows each instance of a class / object that has been created from the class to have a separate copy of the instance variable per object?

So an instance variable is useful for defining the properties of a class / data-type e.g a House would have a location, but now when would I use a class variable in a House object? or in other words what is the correct use of a class object in designing a class?

public class InstanceVaribale {
public int id; //Instance Variable: each object of this class will have a seperate copy of this variable that will exist during the life cycle of the object.
static int count = 0; //Class Variable: each object of this class will contain a single copy of this variable which has the same value unless mutated during the lifecycle of the objects.

InstanceVaribale() {
    count++;

}
public static void main(String[] args) {

    InstanceVaribale A = new InstanceVaribale();
    System.out.println(A.count);
    InstanceVaribale B = new InstanceVaribale();
    System.out.println(B.count);
    System.out.println(A.id);
    System.out.println(A.count);
    System.out.println(B.id);
    System.out.println(B.count);    
    InstanceVaribale C = new InstanceVaribale();
    System.out.println(C.count);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T13:21:08+00:00Added an answer on June 8, 2026 at 1:21 pm

    My personal understanding of a class variable is that a single copy of a class variable that has been declared in a class will be declared using the keyword static, and that each object that has been instantiated from the class will contain a single copy of the class variable.

    No. It’s not that “each object will contain a single copy”. A static variable is associated with the type rather than each instance of the type. The instances don’t have the variable at all.

    There’s exactly one variable (assuming you’re only loading it from one classloader) however many instances of the type there are. No instances? Still one variable. A million instances? Still one variable.

    Static variables are mostly useful for constants or constant-alikes – things like loggers, or “the set of valid prices” etc. Things which don’t change over the course of the application. They should almost always be final in my experience, and the type should be an immutable type (like String). Where possible, use immutable collections too for static variables – or make sure the variable is private and that you never mutate the collection within the class.

    You should avoid using static variables to store global changing state. It makes code much harder to test and reason about.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a generic wrapper class for a bunch of classes we have defined
This may sound like a very generic question but here it goes. I have
I want to have a function defined in a superclass that returns an instance
I have a project that is using a generic list of a custom class.
I have a generic class, on which I define a method that should take
Here's the generic class I'm working with: public interface IRepository<T> where T : EntityObject
I have seen generic repository pattern here . I am trying to add generic
I would like to create a custom WPF control that inherits from comboBox. So
I have defined an abstract method in my base class to take in a
I have a class called PlayerList, in in that class i have an ArrayList

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.