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);
 }
}
asked Jan 23, 2014 at 7:45
8
  • Using Access specifier... Commented Jan 23, 2014 at 7:47
  • 2
    I highly suggest starting with a beginner's book on Java or the tutorials from Oracle. They answer these questions and more. Commented Jan 23, 2014 at 7:47
  • go to the link.tutorialspoint.com/java/java_access_modifiers.htm Commented Jan 23, 2014 at 7:48
  • because you've created object represented by Automobile.java (Automobile class) Commented Jan 23, 2014 at 7: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. Commented Jan 23, 2014 at 7:52

4 Answers 4

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.

answered Jan 23, 2014 at 8:01
Sign up to request clarification or add additional context in comments.

Comments

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

answered Jan 23, 2014 at 7:49

Comments

1

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

answered Jan 23, 2014 at 8:08

Comments

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 | ✔ | ✘ | ✘ | ✘
answered Jan 23, 2014 at 8:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.