简体   繁体   中英

Using getter and setter in main method

public class Testing{

    public static void main(String[] args) {
            String test=("test");
            Scanner type = new Scanner(System.in);
            String theTest = type.next();
            switch(theTest){
                case "test":
                    int input = type.nextInt();


                    System.out.println("OK");



        }
}

public class TestingB{
    private int finalInput;
    finalInput=input; //I want to give finalInput the value of input


}

Ok so this code saves the int which is put in by the user after the "test" in the variable "input".

My question is how do I take this variable "input" which contains the int value and update another int value in a different class?

I tried using setter and getter but I can not use it in the main method or did I misunderstand something?

So apart from the main method which runs in a static context. All classes in Java needs to be instantiated, which means creating a new object from the class ( https://www.javatpoint.com/instantiation-in-java ) In java it is done using the new keyword. Like so:

TestingB testingB = new TestingB();

Now when the object is instantiated we not only have a class. We have an object which is made from the class. The class defines the object, but we can make multiple objects from the same class. Think of the class as the recipe which has shaped the dish created from it.

Not that we have covered the difference between class and object we need to talk about the getters and setters that you mention. In a class a value can have getters and setters which allow other objects to set or get their values

A getter for an int might looks like this:

public int getInput() {
   return this.input;
}

A setter which sets the value looks like this:

public void setInput(int input) {
   this.input = input
}

In this case input is called a field and the setter and getter can set or get the field. The "this" keyword used is pointing to the object. So this.input is the field in the top of the class whereas input is just the parameter passed in the signature of the method.

There are several ways to parse a value to an object you might also do it as a parameter in the constructor (like so: https://beginnersbook.com/2013/03/constructors-in-java/ ). But that might be the topic for another question.

Putting the getter and setter together with the code you provided we end up with this:

public class Main {
 
    public static void main(String[] args) {
        String test = "test"; // I removed the parenthesis they have no function
        Scanner scanner = new Scanner(System.in); //I named the scanner as scanner
        String theTest = scanner.next();
        switch (theTest) {
            case "test":
                System.out.println("Please provide a number also");
                int intput = scanner.nextInt();
                TestingB testing = new TestingB();
                testing.setFinalInput(intput);
                System.out.println("OK: The number from testing is: " + testing.getFinalInput()) ;
                break;
        }
    }
}
 

public class TestingB {
    private int finalInput;
 
    public int getFinalInput() {
        return finalInput;
    }
 
    public void setFinalInput(int finalInput) {
        this.finalInput = finalInput;
     }
}


I could see that you have several mistakes in your syntax. Both the switch statement is not closed properly and the way you have put input in the class would not work. I do no know which editor or IDE you use but I think picking a good one could help you improve. If you are leaning you could consider BlueJ or Greenfoot for java. But even a more complex IDE like IntelliJ will help you. Even though it seems complicated getting used to seeing code in color and having some basic auto complete helps keep the feedback loop from you try something new to you understand if it is correct shorter, and that ultimately help make the learning curve less steep.

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