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?
Static, non-static, final
Moderator: admin
-
- Posts: 9
- Joined: Fri Apr 14, 2023 2:20 pm
- Contact:
Online
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.
-
- Site Admin
- Posts: 10319
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: Static, non-static, final
You understanding is incorrect.hin1129 wrote: ↑Wed May 03, 2023 12:41 pmstatic 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?
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.
Who is online
Users browsing this forum: No registered users and 1 guest