Forums Register Login

An easy class/object/importation question.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Thank you Thomas, but in this sample code the guy didn't import the class and still used the short name for Account?
+Pie Number of slices to send: Send
 

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.
Can you smell this for me? I think this tiny ad smells like blueberry pie!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 805 times.
Similar Threads
Having trouble understanding an error code i keep getting. Please help!
Bank program class or switch problem
How to get a transfer function to work both ways?
Just Can't figure it out and it's due today
Help on testprogram and subclass please
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:46:45.