1

I am new to Java.This was a example on OOP.I have a class file named "Automobile.java" and the driver program saved as AutomobileTest.java.My question is when a object is created in AutomobileTest.java how does it know that it has to access the methods and variables from Automobile.java.
This is my code:
Automobile.java

class Automobile{
    public int numWheels;
    public String colour;
    public boolean engineRunning;
    public double mileage;
    public int numSeats;


    public Automobile(int wheels,int seats,String vehicleColour){
        numWheels=wheels;
        numSeats=seats;
        colour=vehicleColour;
        engineRunning=false;
        mileage=0.0;    
    }

    public void startEngine(){
         engineRunning=true;
    }
    public void stopEngine(){
         engineRunning=false;
    }
    public double drive(double numMiles){
        return mileage+=numMiles;

    }
    public String toString(){
        String s="numWheels="+numWheels+"\nNumber of seats = "+numSeats+ "\nColour:" +colour+ "\nEngineRunning: " + engineRunning;
        return s;
    }
}

AutomobileTest.java

public class AutomobileTest{
    public static void main (String args[]){
        Automobile ferrari=new Automobile(4,2,"red");
        System.out.println(ferrari);
        System.out.println("Engine started");
        ferrari.startEngine();
        System.out.println(ferrari);
    }
}
clarkson
  • 539
  • 1
  • 13
  • 30
  • Using Access specifier... – Sureshkumar Panneerselvan Jan 23 '14 at 07:47
  • 2
    I highly suggest starting with a beginner's book on Java or the tutorials from Oracle. They answer these questions and more. – Brian Roach Jan 23 '14 at 07:47
  • go to the link.http://www.tutorialspoint.com/java/java_access_modifiers.htm – Kamlesh Arya Jan 23 '14 at 07:48
  • because you've created object represented by _Automobile.java_ (`Automobile class`) – Baby Jan 23 '14 at 07:49
  • I suggest you to read Head First java and your will get your answer. But for now , You are creating object of AutomoBile class in your AutoMobileTest.java. – MS- Jan 23 '14 at 07:52
  • You don't have a class file `Automobile.java`. You have a source file of that name. To get a class file `Automobile.class` you need to compile it first. Note, this is not nitpicking, it is important to get not confused about what is what. – Ingo Jan 23 '14 at 07:55
  • Because a) you created a new Automobile (called `ferrari`) in AutomobileTest, and b) the methods and variables in Automobile are public, therefore you can call them from other classes (e.g. by doing `ferrari.startEngine()`) – Trisha Jan 23 '14 at 07:55
  • Strange question. It “knows” because you told it so. Maybe you try to explain what you think could stop it from “knowing”. – Holger Jan 23 '14 at 08:46

4 Answers4

2

From your question I totally understand that you are new to Java programming.

To answer your question, in the AutomobileTest.java file, you have included the statement

Automobile ferrari=new Automobile(4,2,"red");

in this line, the keyword "new" will tell the java compiler to create a object of the class Automobile and thus java compiler will know which class to access.

To know more about this, you need to do a lot of study. Refer this book.

Harish Talanki
  • 790
  • 12
  • 24
1

If you are asking how the contents of AutomobileTest.java knows to find the Automobile class in Automobile.java, this has to do with Java packaging.

In Java, you can declare classes to be of a certain package by saying package X at the top of multiple Java source files. This means that they will share a "namespace", and thus have access to methods and variables (subject the access modifiers that other commenters are mentioning, such as public and private).

But it seems like your files don't say package X at the top. Well, by default Java puts files in the same directory in the "anonymous package", so technically Automobile.java and AutomobileTest.java share a namespace.

For more information about Java packages, see these resources: http://en.wikipedia.org/wiki/Java_package http://docs.oracle.com/javase/tutorial/java/package/packages.html

jaynp
  • 3,045
  • 3
  • 26
  • 40
1

It knows, because they are in the same package. Read about packages and access protection here.

josephus
  • 8,148
  • 1
  • 35
  • 53
0

Have a look at this question: In Java, difference between default, public, protected, and private

Modifier    | Class | Package | Subclass | World
————————————+———————+—————————+——————————+———————
public      |  ✔    |    ✔    |    ✔     |   ✔
————————————+———————+—————————+——————————+———————
protected   |  ✔    |    ✔    |    ✔     |   ✘
————————————+———————+—————————+——————————+———————
no modifier |  ✔    |    ✔    |    ✘     |   ✘
————————————+———————+—————————+——————————+———————
private     |  ✔    |    ✘    |    ✘     |   ✘
Community
  • 1
  • 1
copakawusau
  • 135
  • 9