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 8893899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:21:47+00:00 2026-06-14T23:21:47+00:00

class MyObject { static int instanceCounter = 0; static int counter = 0; MyObject()

  • 0
class MyObject {

static int instanceCounter = 0;

static int counter = 0;

MyObject() {

instanceCounter++;

counter = counter + 1;

}

}

I am using the static ints to get this output:

Value of instanceCounter for Object 1: 5

Value of instanceCounter for MyObject: 5

Value of Counter for Object 1: 1

Value of Counter for Object 2: 2

Value of Counter for Object 3: 3

Value of Counter for Object 4: 4

Value of Counter for Object 5: 5

but its displaying

Value of instanceCounter for Object 1: 5

Value of instanceCounter for MyObject: 5

Value of Counter for Object 1: 5

Value of Counter for Object 2: 5

Value of Counter for Object 3: 5

Value of Counter for Object 4: 5

Value of Counter for Object 5: 5

my runner class

class RunMyObject {

public static void main(String[] args) {

MyObject Object1 = new MyObject();

MyObject Object2 = new MyObject();

MyObject Object3 = new MyObject();

MyObject Object4 = new MyObject();


MyObject Object5 = new MyObject();

System.out.println(“Value of instanceCounter for Object 1: ” + Object1.instanceCounter);

System.out.println(“Value of instanceCounter for MyObject: ” + MyObject.instanceCounter);

System.out.println(“Value of Counter for Object 1: ” + Object1.counter);

System.out.println(“Value of Counter for Object 2: ” + Object2.counter);

System.out.println(“Value of Counter for Object 3: ” + Object3.counter);

System.out.println(“Value of Counter for Object 4: ” + Object4.counter);

System.out.println(“Value of Counter for Object 5: ” + Object5.counter);

}

}

and if i remove static this is what it displays

Value of instanceCounter for Object 1: 5

Value of instanceCounter for MyObject: 5

Value of Counter for Object 1: 1

Value of Counter for Object 2: 1

Value of Counter for Object 3: 1

Value of Counter for Object 4: 1

Value of Counter for Object 5: 1

  • 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-14T23:21:49+00:00Added an answer on June 14, 2026 at 11:21 pm

    Since instanceCounter is a static variable, all objects share the same variable. Since you are incrementing the instanceCounter during each object construction, at the end of creating 5 objects, its value is 5.
    Consequently you get the output as 5 in all your sys outs. Thats the point of static

    EDIT
    To achieve what you need, do the following:

    class MyObject  {
    
        static int instanceCounter = 0;
    
        int counter = 0;
    
        MyObject() 
        {
            instanceCounter++;
            counter = instanceCounter;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function like this in a static class public static IEnumerable<MyObject> getFilteredList(int
My object hierarchy is like this: class Detail { public List<Row> Rows { get;
I've created a domain class in Grails like this: class MyObject { static hasMany
I have the following attempt at an immutable object: class MyObject { private static
I have this class: private static class ClassA{ int id; String name; public ClassA(int
Here is my object constructor static class Edge { int source; // source node
Imagine I have an entity: public class MyObject { public string Name { get;
Suppose I have the following: public class MyObject { public string Name {get; set;}
I have a class class ObjPool { MyObject getObject() {...} void returnObject() {...} int
My object looks like: public class Template { public string Title {get;set;} public string

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.