Page 1 of 1

[HD Pg 0, Sec. 1.5.1 - static-and-instance]

Posted: Mon Dec 16, 2019 6:11 pm
by arganesh3
How a static method can be accessed with a variable of the class?

Re: [HD Pg 0, Sec. 1.5.1 - static-and-instance]

Posted: Mon Dec 16, 2019 10:31 pm
by admin
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.

Re: [HD Pg 0, Sec. 1.5.1 - static-and-instance]

Posted: Tue Dec 17, 2019 3:09 pm
by arganesh3
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.

Re: [HD Pg 0, Sec. 1.5.1 - static-and-instance]

Posted: Tue Dec 17, 2019 9:36 pm
by admin
"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.

Re: [HD Pg 0, Sec. 1.5.1 - static-and-instance]

Posted: Wed Dec 18, 2019 11:20 am
by arganesh3
Thanks for the clarification!