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: Setting a non static instance variable to a static variable in a constructor

  1. #1
    Junior Member
    Join Date
    Jun 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Setting a non static instance variable to a static variable in a constructor

    Example:
    class Demo {
    	private static int increment = 1;
    	public int number;
     
    	public Demo() {
    		number = increment;
    		increment++;
    	}
    }
     
    class App {
     
    	public static void main(String[] args) {
     
    		Demo demo1 = new Demo();
    		System.out.println("Demo1 value: " + demo1.number);
    		Demo demo2 = new Demo();
    		System.out.println("Demo2 value: " + demo2.number);
    	}
    }

    Output:
    Demo1 value: 1
    Demo2 value: 2

    I thought that there was always only one copy of a static variable, but how come in the output the values for each object is different? Is there any way to set both of their numbers to the same static value?

  2. #2
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Setting a non static instance variable to a static variable in a constructor

    how come in the output the values for each object is different?
    The value of increment is changed in the constructor.

    way to set both of their numbers to the same static value?
    Yes remove the statement that changes the value of the increment variable.
    // 		increment++;

Similar Threads

  1. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum Java Theory & Questions
    Replies: 4
    Last Post: July 19th, 2013, 09:06 AM
  2. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 18th, 2013, 12:12 PM
  3. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 03:24 AM
  4. Help setting a private static class variable
    By kyuss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 26th, 2010, 08:09 AM

Tags for this Thread