File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to import custom made class in source file

 
sarah sturgeon
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12020
25
Chrome Java Linux
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sarah sturgeon
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47258
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic