Results 1 to 13 of 13
  1. #1
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Using setters and getters between 3 classes

    I know this is a basic question but I really struggle with this :(

    First class:

    Java Code:
    public class A{
    
    
    C c= new C();
    B b= new B();
    
    public static void main(String[] args) {
        b.start();
        System.out.println(c.getSomething());
        }
    }
    Second Class:
    Java Code:
    public class B{
    
    
    C c= new C();
    
    public void start(){
       c.setSomething(2);
        }
    }
    Third class:

    Java Code:
    public class C{
    
    
    int x;
    
    
      public int getSomething() {
        return x;
    }
    public void setSomething(int x) {
        this.x = x;
    }
    }
    RESULT: 0

    I know I make a new object in class A thats why the sysout returns 0. How can I make it so that I GET the value 2 instead of null in class A, and that I'm able to SET things in class B. So I stay at the same object so to say.

    I just want to be able to SET things in class B and GET that same value from the setter-getter-class-C, in class A. Please help

    Thanks in advance, Jimme

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using setters and getters between 3 classes

    How can I make it so that I GET the value 2 instead of null in class A,
    You shouldn't be getting null with int variables. Can you copy the program's output and paste it here to show what you are talking about.

    The C object in class A has not had the value of its x variable set. Call it's setSomething() method to set it, before calling the getSomething method.
    The C object in class B is separate from the one in A. Each will have its own value in x.


    Also posted at: https://coderanch.com/t/672080/java/...lasses#3142580
    Last edited by Norm; 10-27-2016 at 08:25 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Using setters and getters between 3 classes

    The result is 0. literally, but I want to get number 2, cos I also set number 2 in class B.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using setters and getters between 3 classes

    Call the setSomething method with the value you want x to have.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Using setters and getters between 3 classes

    I did that:

    Java Code:
    public class B{
     
     
    C c= new C();
     
    public void start(){
       c.setSomething(2);
        }
    }
    if I would call the c.getSomething(); in that class I would get 2 as return value, but NOT if I do it in class A, the question is why? :( I really dont understand..

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using setters and getters between 3 classes

    From post#2
    The C object in class A has not had the value of its x variable set. Call it's setSomething() method to set it, before calling the getSomething method.
    The C object in class B is separate from the one in A. Each will have its own value in x.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Using setters and getters between 3 classes

    Thanks you understand what I mean :D But how can I get that same object where the x is set in class A? Because these are just 3 example classes, my other 3 classes are really big.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using setters and getters between 3 classes

    how can I get that same object where the x is set in class A
    If the C object you want is defined in class B, there needs to be a getter method in class B that A can call to get the reference to C.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Using setters and getters between 3 classes

    I just dont see the point in using getters and setters if you can only set and get the same object in 1 class, and not pass the object between different classes

  10. #10
    JavaJimme is offline Member
    Join Date
    Nov 2012
    Posts
    36
    Rep Power
    0

    Default Re: Using setters and getters between 3 classes

    I think this is where polymorphism and inheritance comes into play?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Using setters and getters between 3 classes

    pass the object between different classes
    That is what the getter in B would do: pass its C object to A. Then B and A could both refer to the same C object.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Using setters and getters between 3 classes

    Java Code:
    public class A{
     
     
    C c= new C();
    B b= new B();
     
    public static void main(String[] args) {
        b.start();
        System.out.println(c.getSomething());
        }
    }
    Is there a typo here or something?
    That code won't compile as you don't have a 'b' variable declared that is visible inside main().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Using setters and getters between 3 classes

    To answer you question about getters and setters, their purpose is to basically separate function from implementation
    using a standard naming convention. This provides flexibility on how the value is stored. All the user of your class knows
    is they get a value, not how the value is stored internally. It also permits one to alter the implementation of a published
    class without breaking those applications that use 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. problem with getters and setters
    By Tony Stark in forum New To Java
    Replies: 9
    Last Post: 08-29-2016, 08:09 PM
  2. Getters and setters
    By Erre in forum New To Java
    Replies: 10
    Last Post: 10-25-2012, 08:30 PM
  3. Replies: 7
    Last Post: 02-02-2010, 11:01 AM
  4. Getters and Setters
    By lheviathan in forum New To Java
    Replies: 4
    Last Post: 11-02-2009, 01:47 AM
  5. Getters and Setters
    By Charliestons in forum New To Java
    Replies: 10
    Last Post: 09-12-2008, 10:57 PM

Tags for this Thread

Posting Permissions

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