• Post Reply Bookmark Topic Watch Topic
  • New Topic

An easy class/object/importation question.  RSS feed

 
John Simer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, i was reading a tutorial. The guy made an Account class.


And hopefully i understand this. In the next class he made an Account object named my_account by calling the constructor Account() and then changed the double amount by calling some methods -"my_account.method().



But here's my question. He made the Account class and he made the AccountDemo main class. How does the AccountDemo know the Account class information without ever importing the Account class? Does it automatically know if its in the same folder or directory, or are you supposed to import it?

Thanks and God Bless you helping people.

Matthew 1:23 - "Behold, the virgin shall be with child, and bear a Son, and they shall call His name Immanuel," which is translated, "God with us."
Merry Christmas (or Happy Holidays) and God Bless you all.
 
Stephan van Hulst
Saloon Keeper
Posts: 7961
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, do you know about packages yet?

These two classes are in the same package, and hence are visible to each other without having to use an import statement.
 
John Simer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes I do know about packages.
At the top of the code on both of them, wouldn't you have to put

package nameOfThePackageTheseAreBothIn;

Or do you just never have to say what package it is in?

Thanks
God Bless you, the Lord Jesus has great plans for you
 
Walter Ho
Greenhorn
Posts: 22
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Simer wrote:yes I do know about packages.
At the top of the code on both of them, wouldn't you have to put

package nameOfThePackageTheseAreBothIn;

Or do you just never have to say what package it is in?

Thanks
God Bless you, the Lord Jesus has great plans for you


because you did not specify a package, your classes get associated with the default package.
All classes in the default package are able to see each other as you have it defined.
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the AccountDemo know the Account class information without ever importing the Account class?


Great question! Import statements are not what makes a class available to your program, they just make it possible for you to use the class's short name. The classpath is what makes a class available. If the com.acme.Monkey class is in the classpath, you can say

com.acme.Monkey curious = new com.acme.Monkey();

but if you import com.acme.Monkey you can say

Monkey curious = new Monkey();

and the compiler will know what you mean (unless there's another Monkey class somewhere). The compiler still records it using the full classname.
 
John Simer
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Thomas, but in this sample code the guy didn't import the class and still used the short name for Account?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Simer wrote:Thank you Thomas, but in this sample code the guy didn't import the class and still used the short name for Account?


Because the other class is in the same package--that is, the "default" package, or no package at all.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!