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: Value of a variable not changing after reinstantiating it

  1. #1
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Value of a variable not changing after reinstantiating it

    Hello,
    I was messing around testing out my understanding of Generics when I came across a problem that I believe to be simple, yet I cannot recall the solution to it. At first, I had the value hold an Integer type value of 9. Then I instantiated the value to 9.0, changing its value type to Double. Now I'm trying to change the value of it again, keeping it the same type, but getting it from 9.0 to 12.3. I know that probably adding a constructor would do the job, but I wonder if there's a way to change the value without having to include a constructor in the 'Container' class.

    What I've tried:
    So I know adding a constructor makes the change work. Also, not adding a constructor but making the 'N value' a field, then using the Setter/Getter would work. Just wondering if there's another way/explanation behind why this is occurring.

    My question:
    What is/isn't happening that the value of 'value' isn't changing from 9.0 to 12.3 as when I print out it towards the end, it is still 9.0 compared to the 9 to 9.0 change?

    Edit: Fixed, was a simple logic error.

    Here is my code below:
    class Container<N extends Number>{
     
    	N value;
     
    	public N getValue() {
    		return value;
    	}
     
    	public void setValue(N value) {
    		this.value = value;
    	}
     
    	//Simple to show of what type our value holds
    	public void showType() {
    		System.out.println(value.getClass().getName());
    	}
    }
     
    public class PracticeGenerics {
     
    	public static void main(String[] args) {
     
    		//We create our 'Container' object not using Generics
    		Container obj = new Container();
     
    		//Since our class doesn't have a constructor, we instantiate the value of 'value' to 9 here
    		obj.value = 9;
    		//We display the 'value' type here
    		obj.showType();
    		System.out.println(obj.value);
    		obj.value = 9.0;
    		obj.showType();//This displays 'java.lang.Double' now instead of 'java.lang.Integer'
    		System.out.println(obj.value);//Prints out the value of 'value' which we changed from an int type '9' to a double type '9.0'
     
    		//Separate the two different tests
    		System.out.println("---------------------------------------------");
     
    		//Creating our 'Container' object using Generics
    		Container<Double> obj2 = new Container<Double>();
     
    		obj2.value = 12.3;
    		obj2.showType();
    		System.out.println(obj2.value);
     
    	}//end Main
     
    }//end Class
    Last edited by HyperRei; September 12th, 2021 at 02:01 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Value of a variable not changing after reinstantiating it

    Can you copy the program's output and post it here with some comments against the line(s) where the output is not what you expected?

    It looks like the compiler generates code to create an object from the number and uses it to assign it to the variable: value.

    isn't changing from 9.0 to 12.3
    Which object are you asking about? obj or obj2

    NOTE: OP just changed code: replaced obj with obj2 in the following:
    	        obj2.showType();
    		System.out.println(obj2.value);
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Changing Variable Within Method
    By Deployment in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2017, 10:23 PM
  2. help with defining a changing variable within a loop
    By uswhovian in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 9th, 2013, 11:30 AM
  3. help with defining a changing variable within a loop
    By uswhovian in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 9th, 2013, 10:30 AM
  4. help with defining a changing variable within a loop
    By uswhovian in forum Loops & Control Statements
    Replies: 3
    Last Post: March 9th, 2013, 10:30 AM
  5. calling a changing variable from another class
    By bondage in forum Collections and Generics
    Replies: 11
    Last Post: December 7th, 2011, 10:17 AM

Tags for this Thread