posted 6 years ago
Hi every body.
I have few questions
.
Let say we created a class named Car in our source file.
In the same source file I want to create two objects car1,car2, based on class Car( that is i want to create two instances of class Car)
Can i create these objects as shown below:
Car car1 = new Car();
Car car2= new Car ( );
My second question is let say i am writing a different java program in a separate source file. I feel the need two create two objects car3 and car4 based on class Car.
1) In order to create these objects car3 and car4 in my program , the class Car must be imported in my source file . is it correct?
If correct, how do we do that ? I know for classes in API, we use import command.
I apologize for the long-winded post
Thanks and have a great day
I have few questions
.
Let say we created a class named Car in our source file.
In the same source file I want to create two objects car1,car2, based on class Car( that is i want to create two instances of class Car)
Can i create these objects as shown below:
Car car1 = new Car();
Car car2= new Car ( );
My second question is let say i am writing a different java program in a separate source file. I feel the need two create two objects car3 and car4 based on class Car.
1) In order to create these objects car3 and car4 in my program , the class Car must be imported in my source file . is it correct?
If correct, how do we do that ? I know for classes in API, we use import command.
I apologize for the long-winded post
Thanks and have a great day
posted 6 years ago
-
1
-
-
-
-
Java would not be a very useful language if you couldn't create more than one instance of a class.
Think of your class as a blueprint, and the instance as a house. You draw up the blueprints once. You can then use those blueprints to make as many copies of the house as you want. Each house has its own address, owner, bedroomWallColor, etc. So while each one looks and acts more or less the same, each maintains its own set of values for the various things it stores.
Think of your class as a blueprint, and the instance as a house. You draw up the blueprints once. You can then use those blueprints to make as many copies of the house as you want. Each house has its own address, owner, bedroomWallColor, etc. So while each one looks and acts more or less the same, each maintains its own set of values for the various things it stores.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
sarah sturgeon
Greenhorn
Posts: 11
posted 6 years ago
Thanks Fred.
Ok i understand the relationship between objects and its class.
However , i am not cleared how to create objects from a class.
http://download.oracle.com/javase/tutorial/java/concepts/class.html
The above link illustrates how the objects are created from a class
Following the above link, can i create two objects car1 and car2 from class Car as shown below
Car car1 = new Car ( );
Car car2= new Car ( ) ;
is this correct understanding ?
Second thing How do i import user-created class in my source file if not defined in it ?
Thanks for all your help.
Thanks Fred.
Ok i understand the relationship between objects and its class.
However , i am not cleared how to create objects from a class.
http://download.oracle.com/javase/tutorial/java/concepts/class.html
The above link illustrates how the objects are created from a class
Following the above link, can i create two objects car1 and car2 from class Car as shown below
Car car1 = new Car ( );
Car car2= new Car ( ) ;
is this correct understanding ?
Second thing How do i import user-created class in my source file if not defined in it ?
Thanks for all your help.
posted 6 years ago
Yes, in those two lines, you are creating two Car objects from the Car class.
If the classes are in the same package (probably they are, since you are very new to Java - you've probably not done anything with packages yet), then you don't have to do anything. As long as the files for the classes are in the same directory, Java will find the other class automatically.
Only if it is in another package. If class Car is in the same package as the class you're just writing, you don't need to import it; Java will find it automatically.
-
2
-
-
-
-
sarah sturgeon wrote:Following the above link, can i create two objects car1 and car2 from class Car as shown below
Car car1 = new Car ( );
Car car2= new Car ( ) ;
is this correct understanding ?
Yes, in those two lines, you are creating two Car objects from the Car class.
sarah sturgeon wrote:Second thing How do i import user-created class in my source file if not defined in it ?
If the classes are in the same package (probably they are, since you are very new to Java - you've probably not done anything with packages yet), then you don't have to do anything. As long as the files for the classes are in the same directory, Java will find the other class automatically.
Jean Fontaine wrote:You car class does need to be imported for you to create an instance.
Only if it is in another package. If class Car is in the same package as the class you're just writing, you don't need to import it; Java will find it automatically.
