0

I have just started coding in java and am trying to wrap my head around classes.

I seem to be having trouble using a public attribute in another class. I have 3 classes: one contains the main method; and the other 2 are input and output. I am using non-static variables and methods and I don't want to use static.

Even though I have instantiated the input class in the output class, the output class fails to recognize the public attribute. Why is that so?

Here are the 3 classes:

package random;
import java.util.Scanner;
import java.util.Arrays;

public class random
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Please enter your first name: ");  
        input inputObject = new input();
        inputObject.setFirstName();
        output outputObject = new output();
        outputObject.getFirstName();
    }
}

package random;

import java.util.Scanner;
public class input
{
     public String firstName;
     public input() {}
     public void setFirstName() {
        Scanner keyboard = new Scanner(System.in);
        this.firstName = keyboard.nextLine();
    }
}

package random;
import java.util.Scanner;

public class output
{
    public void getFirstName() 
    {
        input inputObject = new input();
        System.out.println("Your first name is " + inputObject.firstName);
    }
}
1
  • If you really want your code to act in that fashion where you can share a single instance of a class/object look into using the Singleton pattern. Other than that what the others have mentioned is correct as you are using 2 different instantiations of the input object. What you should do is use a single class that contains the attributes and getters and setters of like things such as attributes of a user or person like used in your example above with first name. Then you would instantiate the class once and use the getters and setters to manipulate the object.
    – rmeier
    Oct 7, 2015 at 3:44

2 Answers 2

1

The input object created in the getFirstName method in the output class is a separate instance from the one created in your main method. This means essentially you are creating a new instance of input where the input isn't set for firstName yet so when you print out that property there is nothing to print.

In addition the get and set methods for a single property should be inside the same class. The general way of doing this is like this:

class Foo {

    private String property = "";

    Foo () {}; //empty default constructor

    //sets the property to what is passed in as a parameter
    public void setProperty(String newProperty) {
        this.property = newProperty;
    }

    //returns the property
    public String getProperty() {
        return this.property;
    }
}

Generally all class properties should be private with public setters and getters. If you are new to programming I suggest reading up on this more.

0

It's because you're using two different input objects. We can tell this because your program has the following line twice

input inputObject = new input();

The input object you assign a name to is a different object from the one you try to read the name from.

It makes no sense for the class output to exist at all. You should just have a method getFirstName() in the input class.

(Also class names in Java usually begin with a capital letter.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.