Page 1 of 1

Static, non-static, final

Posted: Wed May 03, 2023 12:41 pm
by hin1129
i am having trouble with Static and non-static methods/variables.
please correct me if any of the below is wrong:
static variables can only be used in Static methods
non-static (instance) variables can be used in both Static and non-static methods
when final is assigned to a variable, the value became unchangeable. in this case, can it be used in both Static and non-static methods?

Re: Static, non-static, final

Posted: Thu May 04, 2023 1:39 am
by admin
hin1129 wrote:
Wed May 03, 2023 12:41 pm
static variables can only be used in Static methods
non-static (instance) variables can be used in both Static and non-static methods
when final is assigned to a variable, the value became unchangeable. in this case, can it be used in both Static and non-static methods?
You understanding is incorrect.
The issue is not which type of variable can be accessed by which type of method. The issue is about the understanding of what static and non-static means. To access a static variable you don't need a reference to an object of the class (of which the static variable is a member) but to access a non-static variable, you need a reference to an object because non-static variables belong to an object.

You really need to read a book thoroughly because it is an important concept and is not possible to explain all the details in a forum post.