Printing variable of class level if local and class level is having same name. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Printing variable of class level if local and class level is having same name.

What if there are two variable with the same name one is local and other is at class level. If I want to print class level variable then how can i ?

6th Aug 2019, 9:07 PM
Neeraj Thakur
Neeraj Thakur - avatar
2 Answers
+ 2
Assuming you’re talking about java, the “this” keyword will allow you to reference a current class variable. https://www.javatpoint.com/this-keyword public class Program { String s = "This is an instance variable"; void myMethod() { String s = "this is a local variable"; System.out.println(this.s); System.out.println(s); } public static void main(String[] args) { new Program().myMethod(); } }
6th Aug 2019, 9:20 PM
Jake
Jake - avatar
0
Do you have any code example?
6th Aug 2019, 9:11 PM
Jakub ⚡
Jakub ⚡ - avatar