[HD Pg 0, Sec. 1.5.1 - static-and-instance]
Moderator: admin
-
- Posts: 3
- Joined: Mon Dec 16, 2019 6:06 pm
- Contact:
[HD Pg 0, Sec. 1.5.1 - static-and-instance]
How a static method can be accessed with a variable of the class?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 0, Sec. 1.5.1 - static-and-instance]
Did you read point 2 under "Important points about static"? It shows exactly how:
Even though static fields belong to a class and should be accessed through the name of the class, for example, Account.numberOfAccounts, it is not an error if you access it through a variable of that class, i.e., acct1.numberOfAccounts.
-
- Posts: 3
- Joined: Mon Dec 16, 2019 6:06 pm
- Contact:
Re: [HD Pg 0, Sec. 1.5.1 - static-and-instance]
Yes, I read that but not able register in mind. As I always refer it (acct1. or acct2.) as Object or Instance of Class. Not used to be calling it as a variable. Somehow when I read variable i think (int i) only. Is reference variable the right term? Correct me if my assumption is wrong.
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: [HD Pg 0, Sec. 1.5.1 - static-and-instance]
"Reference variable" is the right term. A reference variable points to an object. A primitive variable does not point to any object. But both are variables. Please go through Section 1.4 "Object and Reference" again, carefully. It is explained in detail there.
-
- Posts: 3
- Joined: Mon Dec 16, 2019 6:06 pm
- Contact:
Re: [HD Pg 0, Sec. 1.5.1 - static-and-instance]
Thanks for the clarification!
Who is online
Users browsing this forum: Bing [Bot] and 7 guests