简体   繁体   中英

How can I access variables from 2 different classes in Java

I have 3 different classes in Java. FirstClass that has 2 String s, ThirdClass that has 1 String , and SecondClass that needs access to those Strings. How can I access all of these Strings?

If there were 2 classes I would have done something like the following

    public class FirstClass {
        private String name;
        private String lname;
    
        public FirstClass(){
            name = "John";
            lname = "Watson";
            new SecondClass(name, lname);
        }
    }
    public class SecondClass{
         new SecondClass(String name, String lname){
              //doSomething
          }
    }

How can I have a ThirdClass with another String accessible from SecondClass ?

I've searched a lot and nothing came up other than the solution with 2 classes. I don't want to do the following

    public class FirstClass{
        private String name;
        private String lname;
    
        public FirstClass(){
            name = "John";
            lname = "Watson";
            new ThirdClass(name, lname);
        }
      }
    public class ThirdClass{
        private String location;
        public ThirdClass(String name, String lname){
            location = "Europe";
            new SecondClass(name, lname, location);
        }
    }
    public class SecondClass{
         new SecondClass(String name, String lname, String location){
              //doSomething
          }
    }

I also can't put all the Strings in one class. Is there a way that I can do it?

EDIT : removed first awful attempt...

I did some refactoring of the code and came up with the following:

class FirstClass {

    private String name;
    private String lname;

    public FirstClass() {
        name = "John";
        lname = "Watson";
    }

    public String getName() {
        return name;
    }

    public String getLName() {
        return lname;
    }
}

class SecondClass {

    // composition of classes - promotes loose coupling
    private FirstClass firstClass;
    private ThirdClass thirdClass;

    /**
     * Constructor 1 - takes only a FirstClass and 
     * delegates the assignment to Constructor 2 
     * A class can have more than one constructor 
     * so long as the signatures are unique
     * @param firstClass
     */
    public SecondClass(FirstClass firstClass) {
        this(firstClass, null);
    }

    /**
     * Constructor 2 - takes two inputs FirstClass AND ThirdClass
     * Any calls to get the methods are delegated to the classes
     * this promotes loose coupling
     * @param firstClass
     * @param thirdClass
     */
    public SecondClass(FirstClass firstClass, ThirdClass thirdClass) {
        this.firstClass = firstClass;
        this.thirdClass = thirdClass;
    }

    public String getName() {
        // delegate getName to firstClass
        return firstClass.getName();
    }

    public String getLName() {
        // delegate
        return firstClass.getLName();
    }

    public String getLocation() {
        // delegate
        return thirdClass.getLocation();
    }
}

class ThirdClass {

    private String location;

    public ThirdClass(String location) {
        location = "Europe";
    }

    public String getLocation() {
        return location;
    }
}

You need to include the object references and then wrap (chain) the getter calls to return the results of other getters.

A better solution will be how @Yousaf explained. Quoting him

You need to create an instance of FirstClass and ThirdClass in SecondClass to access the private instance fields from FirstClass and SecondClass, you can create getter and setter functions in FirstClass and ThirdClass

First class

public class FirstClass{
    private String name;
    private String lname;

    public FirstClass(){
        name = "John";
        lname = "Watson";
    }
    
    public String getName() {
       return this.name;
    }

    public String getLName() {
       return this.lname;
    }
}

Third Class

public class ThirdClass{
    private String location;
    public ThirdClass(String name, String lname){
        location = "Europe";
    }

    public String getLocation() {
       return this.location;
    }
}

Second Class

public class SecondClass{
     private FirstClass firstClass;
     private ThirdClass thirdClass;
     new SecondClass(){
          firstClass = new FirstClass();
          thirdClass = new ThridClass();

          String name = firstClass.getName();
          String LName = firstClass.getLName();
          String location = thirdClass,getLocation();
      }
}

Note that I am creating instances of object in the constructor of the code but you don't have to do that. You can create then in the main method and pass them to SecondClass as a parameter, and that would work as well.

It would look something like this Main Method

public static void main(String[] args) {
   FirstClass firstClass = new FirstClass();
   ThirdClass thirdClass = new ThridClass();
   SecondClass secondClass = new SecondClass(firstClass, thirdClass);
}

Second Class

public class SecondClass{
     private FirstClass firstClass;
     private ThirdClass thirdClass;
     new SecondClass(FirstClass firstClass, ThirdClass thirdClass){
          this.firstClass = firstClass;
          this.thirdClass = thirdClass;
      }
}

or you could just do a deep copy. If you want to isolate second class so changes to data from first and third won't affect the second

Second Class

public class SecondClass{
    private String name;
    private String lname;
    private String Location;
     new SecondClass(FirstClass firstClass, ThirdClass thirdClass){
          this.name = firstClass.getName();
          this.lname = firstClass.getLName();
          this.location = thirdClass.getLocation;
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM