• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to import custom made class in source file

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The knights of nee want a shrubbery. And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic