简体   繁体   中英

Java: using object from class in another class

I have two different files. In the file below I create a new object with an attribute and I create a method to retrieve that attribute.

public class Journey
{
    public double singleCost;

    public void setSingleCost(double cost) {
        singleCost = cost;
    }

    public double getSingleCost() {
        return singleCost;
    }

    public static void main (String[] args) {
        Journey leicester_loughborough = new Journey();
        leicester_loughborough.setSingleCost(2.5);

        Journey leicester_nottingham = new Journey();
        leicester_nottingham.setSingleCost(3.5);
    }
}

In a separate file, I would like to print out that object's attribute. I'm not sure how to do that though. The following doesn't seem to work.

public class JourneyMethods
{    
    public static void main (String[] args) {
        System.out.println(leicester_loughborough.getSingleCost());
    }
}

I would appreciate any help, thanks.

You are thinking about this too much as main methods.

Your JourneyMethods main method knows nothing about the Journey class unless you tell it . JourneyMethods will be able to interact with a Journey instance if you create a reference to a Journey .

You simply create a getter()-method for the attribute you want to have in the class that has it (in your case Journey.class). Like this:

public double getSingleCost() {
    return singleCost;
}

You can then get the attribute by calling the getSingleCost() from the object-instance you have. Like this:

double cost = leicester_loughborough.getSingleCost();

Please note that this is really basic stuff and you might want to read some more Java tutorials first.

Oh, and your program is only supposed to have one main() method as entry point. (there are reasons to have more than one but those are very case-specific). If you run your program in the way it's written in the OP, the address spaces as well as the execution of the two applications are seperated from each other and you do not have a means to access objects that are contained in a different application (well, there are ways like serialization, but those are more advanced topics). Have your classes/objects contained in the same program.

Edit: Fixing OP's code to work as intended:

public class Journey
{
    public double singleCost;

    public void setSingleCost(double cost) {
        singleCost = cost;
    }

    public double getSingleCost() {
        return singleCost;
    }
}

JourneyMethods:

public class JourneyMethods
{    
    public static void main (String[] args) {
        Journey leicester_loughborough = new Journey();
        leicester_loughborough.setSingleCost(3.5);
        System.out.println(leicester_loughborough.getSingleCost());
    }
}

You need to create the two journey objects in your JourneyMethods class so you have a reference to them. Also you should refer to the 'files' as classes, because 'file' usually implies files on the filesystem, not files representing Java classes. That confused me a bit.

Your class object :

public class Journey
{
    public double singleCost;

    public void setSingleCost(double cost) {
        singleCost = cost;
    }

    public double getSingleCost() {
        return singleCost;
    }

}

Your main class :

public class JourneyMethods
{    
    public static void main (String[] args) {
        Journey Leicester_loughborough = new Journey();
        leicester_loughborough.setSingleCost(2.5);
        Journey leicester_nottingham = new Journey();
        leicester_nottingham.setSingleCost(3.5);
        System.out.println(leicester_loughborough.getSingleCost());
    }
}

This is how Java works, I think that you are confused with C (with .h and .c files) Having a main() function in your Journey class does not make sense, you can instantiate your object from anywhere in your code, so have it in your main() function :)

In Java, you generally want to put methods in the Object class.

To access the object attributes, you have to make the objects in the external class and use getter and setter methods to get the values of the objects.

public class Journey{
public double singleCost;

public void setSingleCost(double cost) {
    singleCost = cost;
}

public double getSingleCost() {
    return singleCost;
}  

}

public class JourneyMethods{    
public static void main (String[] args) {
    Journey Leicester_loughborough = new Journey();
    leicester_loughborough.setSingleCost(2.5);
    Journey leicester_nottingham = new Journey();
    leicester_nottingham.setSingleCost(3.5);
    System.out.println(leicester_loughborough.getSingleCost());
}

}

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