aspose file tools*
The moose likes Beginning Java and the fly likes how to import custom made class in source file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to import custom made class in source file" Watch "how to import custom made class in source file" New topic
Author

how to import custom made class in source file

sarah sturgeon
Greenhorn

Joined: Aug 27, 2011
Posts: 11
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
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10925
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
sarah sturgeon
Greenhorn

Joined: Aug 27, 2011
Posts: 11

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.
Jean Fontaine
Greenhorn

Joined: Apr 12, 2011
Posts: 12
I will probably say something stupid, because I don't speak english very well, but,

Did you try writing import [PackageName].Car; before your code ?

You car class does need to be imported for you to create an instance.

Have a Nice Day !

Oracion
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
You must write the name of the package when using the import keyword, and you can only import code from a package with a name.
 
Don't get me started about those stupid light bulbs.
 
subject: how to import custom made class in source file
 
Similar Threads
hashCode and equals implementation
Using final
TreeSet not allowing the addition of a unique object, yet the equals() method is not being called
SQL question
Creating multi object in a loop without using list