Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Can anyone answer this?

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can anyone answer this?

    Why is the print out 99 instead of 88 since its ob1.var1=88 System.out.println("ob1 integer:"+ob1.var1);

    class JavaExample{
       //Static integer variable
       static int var1=69; 
       //non-static string variable
       String var2;
     
       public static void main(String args[])
       {
    	JavaExample ob1 = new JavaExample();
    	JavaExample ob2 = new JavaExample();
    	/* static variables can be accessed directly without
    	 * any instances. Just to demonstrate that static variables
    	 * are shared, I am accessing them using objects so that 
    	 * we can check that the changes made to static variables
    	 * by one object, reflects when we access them using other
    	 * objects
    	 */
            //Assigning the value to static variable using object ob1
    	ob1.var1=88;
    	ob1.var2="I'm Object1";
            /* This will overwrite the value of var1 because var1 has a single 
             * copy shared among both the objects.
             */
            ob2.var1=99;
    	ob2.var2="I'm Object2";
    	System.out.println("ob1 integer:"+ob1.var1);
    	System.out.println("ob1 String:"+ob1.var2);
    	System.out.println("ob2 integer:"+ob2.var1);
    	System.out.println("ob2 String:"+ob2.var2);
       }
    }

    Output:

    ob1 integer:99
    ob1 String:I'm Object1
    ob2 integer:99
    ob2 String:I'm Object2

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Can anyone answer this?

    Having a static variable means that there is only one copy for that variable no matter how many instances of the class.
    Remove the static attribute from the variable's declaration so that each instance of the class will have its own value.

    Read the comments in the code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] My answer comes out a +0.020 more than the answer in the book
    By UniverseCloud in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 18th, 2019, 08:05 PM
  2. Answer me !
    By johnmecanse34 in forum Java Networking
    Replies: 2
    Last Post: November 20th, 2013, 06:12 AM
  3. Replies: 3
    Last Post: March 9th, 2013, 07:22 PM
  4. What's ur answer?
    By Tanmaysinha in forum The Cafe
    Replies: 6
    Last Post: October 12th, 2011, 06:26 AM
  5. need answer for these
    By satishbs in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2011, 01:46 AM