Results 1 to 10 of 10
  1. #1
    Tony Stark is offline Member
    Join Date
    Aug 2016
    Posts
    26
    Rep Power
    0

    Default problem with getters and setters

    i made 3 classes , the second class i have private variables , setting there values from the first class , then getting them and print them using a method in the third class which i call it in the first class .
    the problem is when i call it it prints variables without values like:
    string = null
    int = 0
    double = 0.0
    althought i gave them values by setters as i said .

    thanks .
    Tony .
    Last edited by Tony Stark; 08-29-2016 at 01:03 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: problem with getters and setters

    Please include a small and complete program that demonstrates the problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Tony Stark is offline Member
    Join Date
    Aug 2016
    Posts
    26
    Rep Power
    0

    Default Re: problem with getters and setters

    MAIN CLASS :

    Java Code:
    import java.util.Scanner ;
    public class main {
    
    	
    	public static void main(String[] args) 
    	{
            class1 vvv = new class1();
    		class2 zzz = new class2();
            Scanner In = new Scanner(System.in);     
    		System.out.println("Please Enter The Color !!");
            vvv.setColor(In.nextLine());
            zzz.Print();
            
    	}
    
    }
    SECOND CLASS :

    Java Code:
    public class class1 
    {
    	
    private String Color ;
    
    void setColor(String c)
    {
    	this.Color = c ;
    }
    String getColor()
    {
    	return Color ;
    }
    THIRD CLASS :
    Java Code:
    public class class2 
    
    {
    class1 xxx = new class1();
    void	Print()
    	{
    		System.out.println("the color is " + xxx.getColor());
    	}
    	
    	
    }
    the output is :
    "the color is Null"

    Note : I know that i can do this in just two classes , i am doing this just for practising .

    thanks .
    Tony

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: problem with getters and setters

    Practicing is good! More folks should do it.

    The problem is that you are trying to get the color from a new instance of Class1. Not the one in
    which you actually set the color. Try passing vvv to your Print method and use that to retrieve
    the color.

    Point to remember! All instances of a given class are different and may hold different values for
    the same fields.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Tony Stark is offline Member
    Join Date
    Aug 2016
    Posts
    26
    Rep Power
    0

    Default Re: problem with getters and setters

    Wooooooow , Look what have i done :

    MAIN CLASS :

    Java Code:
    import java.util.Scanner ;
    
    public class main {
    public static class1 vvv = new class1();
    
    
    	public static void main(String[] args) 
    	{
    		 Scanner In = new Scanner(System.in);
    			class2 zzz = new class2();
    	        System.out.println("Please Enter The Color !!");
    			vvv.setColor(In.nextLine());
    	        zzz.Print();
    	}
    	
    
    
    }
    SECOND CLASS :

    Java Code:
    public class class1 
    {
    	
    private String Color ;
    
    void setColor(String c)
    {
    	this.Color = c ;
    }
    String getColor()
    {
    	return Color ;
    }
    
    }
    THIRD CLASS :

    Java Code:
    public class class2 
    
    {
    
    	void	Print()
    	{
    	main alv = new main();
    	
    	
    	System.out.println("the color is " + alv.vvv.getColor());
    	}
    	
    	
    }

    * Note : I'm The Best .

    Tony .

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: problem with getters and setters

    Well, that's certainly a round about way to do it.

    Note: You didn't need to create an instance of main. vvv is static so you can just
    access it as main.vvv.

    You should also try doing it the way I suggested. Passing an instance of vvv to
    the Print method.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Tony Stark is offline Member
    Join Date
    Aug 2016
    Posts
    26
    Rep Power
    0

    Default Re: problem with getters and setters

    Quote Originally Posted by jim829 View Post
    Passing an instance of vvv to
    the Print method.
    Well I don't really understand what did you mean by "passing" hope that you explain it with an example please ,
    thanks !

    Tony

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: problem with getters and setters

    Quote Originally Posted by Tony Stark View Post
    Well I don't really understand what did you mean by "passing" hope that you explain it with an example please ,
    thanks !
    Java Code:
    public class class2  {
        void  Print(Class1 o) {
          System.out.println("the color is " + o.getColor());
        }
    }
    Now call the above method like this:
    Java Code:
    zzz.Print(vvv);  // passing in instance of Class1 to the method
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Tony Stark is offline Member
    Join Date
    Aug 2016
    Posts
    26
    Rep Power
    0

    Default Re: problem with getters and setters

    o here is an object ?? a parameter ? or something else ??

    thanks ! ,

    Tony

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: problem with getters and setters

    It is both an object and a parameter (or argument) to the method. The argument you are passing is a reference of a
    class (in this case Class1) Yo can then use that reference to access members of that particular instance of the class.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Getters and setters
    By Erre in forum New To Java
    Replies: 10
    Last Post: 10-25-2012, 08:30 PM
  2. Confused about getters and setters
    By jnjh in forum New To Java
    Replies: 8
    Last Post: 05-03-2011, 07:12 AM
  3. Getters and Setters
    By lheviathan in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 01:47 AM
  4. Replies: 1
    Last Post: 02-19-2009, 03:50 AM
  5. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 10:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •