Static, non-static, final

Oracle Certified Foundations Associate Java Certification Questions and Discussion
1Z0-811

Moderator: admin

Post Reply
hin1129
Posts: 9
Joined: Fri Apr 14, 2023 2:20 pm
Contact:

Static, non-static, final

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Static, non-static, final

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests