-2

I'm really stuck on this code because I can't seem to understand how to access data member into another class in java. I need someone to explain this to me properly.

I have my customer class with the following data members:

name
startLocation
endLocation

When I try to access these data members in my separate .java file I get errors at this line:

System.out.println("Driver #" + ID + " has dropped off " + getname + " at " + getendLocation);

I can't just have that code without bringing the data members into the other .java file. Can someone explain how I can take this code and make it visible in my seperate file?

    public class Customer
    {
    private String name;
    private String startLocation;
    private String endLocation;

    public Customer (String name, String startLocation, String endLocation)
    {
        this.name = name;
        this.startLocation = startLocation;
        this.endLocation = endLocation;
    }

    public String getname()
    {
        return name;
    }

    public String getstartLocation()
    {
        return startLocation;
    }

    public String getendLocation()
    {
        return endLocation;
    }
    }

Now here is where I'm stuck. When I went to the tutor center at my school the student butted in to helped me told me I needed to make a main constructor. So I did and this is it. He told me I also needed to add a name = new name in there too, but when I did add that he suddenly told me to forget everything he told me and to go back with my way of doing it (I didn't have a way worked out yet, was just messing around). Can someone finish explaining what he was trying to show me?

    public Customer()
    {
        this.name = name;
        this.StartLocation = startLocation;
        this.endLocation = endLocation;
    }
  • class name should be "Customer", not "customer". Pay attention - these things matter. Getter and setter methods are named properly, either. Small things matter in programming. – duffymo Oct 17 '15 at 01:06
  • Are you asking how to instantiate a class, how to invoke methods an instance? – Sotirios Delimanolis Oct 17 '15 at 01:07
  • 1
    `getName` and `getendlocation` are methods not variables, you call them after you create instance of `Customer` ex: instance.getName() – sam Oct 17 '15 at 01:07
  • 1
    [Because it's all here.](https://docs.oracle.com/javase/tutorial/java/javaOO/) All I had to search for was _java objects_. – Sotirios Delimanolis Oct 17 '15 at 01:07

1 Answers1

0

There are a lot of issues here. Let,s start with the basics:

  1. Read a few tutorials on basics of Java objects and calling methods.
  2. Java is case sensitive - you're going to have a lot of problems if you don't fix that.
  3. Your constructor isn't doing anything useful, since you're not passing in variables. Everything is null.

Your code that uses it will look something like this:

Customer c = new Customer("Jaime", "start", "end");
System.out.println(c.getName());
Krease
  • 15,805
  • 8
  • 54
  • 86
  • Yeah sorry about that. Missed the C when I was re-typing it in my browser. That part is okay in my code. And I'm getting their information from another file. I have the Dispatcher.java, Driver.java, and Customer.java. The Dispatcher asks for their information and I wanted to create the Customer class where it ends up. But thanks for pointing me towards objects. I kept googling about passing data members and the results were all trash. At least now I know what to look for. – Jaime Waters Oct 17 '15 at 01:28