Behavior of local and class variables in java

I am new to Java programming language.
I am familiar with C and C ++, but cannot understand the behavior of the program below.

public class Test { static int x = 11; private int y = 33; public void method1(int x) { Test t = new Test(); this.x = 22; y = 44; System.out.println("Test.x: " + Test.x); System.out.println("tx: " + tx); System.out.println("ty: " + ty); System.out.println("y: " + y); } public static void main(String args[]) { Test t = new Test(); t.method1(5); } } 

Correct output:

 Test.x: 22 tx: 22 ty: 33 y: 44 

Expected Result:

 Test.x: 22 tx: 22 ty: 44 // As variable y is modified inside the function. y: 44 

Even changing the string from y = 44; before this.y = 44; does not give the expected result.

+5
source share
4 answers

The problem is that you do not mean the actual created object. You are passing a variable from another instance that has new variables.

  Test t = new Test(); this.x = 22; y = 44; System.out.println("Test.x: " + Test.x); System.out.println("tx: " + tx); System.out.println("ty: " + ty); System.out.println("y: " + y); 

If you look at the first line Test t = new Test();

you do not call method1 on this particular instance, where y assigns 44. Therefore, you see the top-level value.

It will be more understandable if you rename your instances. Instead of t always.

This is the reason for the confusion, and you call method1() inside, which can lead to an infinite loop.

+1
source

The basic difference between static and non-static variables

 class Student { private int id; private String name; static String collegeName; } 

For each non-static student object, the id and name attributes will be loaded into memory with its initial values ​​(0 and null), the identifier and name may be different for each object. But collegeName will only load once when the class is loaded to execute. Thus, for each Student object there will be the same college name. This means static .

Access to static and non-static variables

 class Student { private int id; private String name; static String collegeName; public static void main(String[] args) { String s1 = Student.collgeName; String s2 = collgeName; Student student = new Student(); String s3 = student.name; int id = student.id; } } 

Static variables can be accessed directly using their name or using the class name. When a static global variable and a local variable exist, static should be used with the class name

 public static void main(String[] args) { String s1 = Student.collgeName; String collgeName = "foo"; String output = collgeName; } 

Here output will be set to " foo ". A local variable always takes precedence over a global static variable, and therefore String output = s1; will give the value for output as null .

Inside the non-static static block, you need to access the variables using reference variables (we need to create an object). The main method is static, so we had to create a Student object to access the id and name values, otherwise this would give a compile-time error.

Blind rule regarding non-static blocks

Each non-static block will use the default keyword this , which displays the current link to which the block is called when class variables (both static and non-static) are used. Java sample code

 class Student { private int id; private String name; static String collegeName; void setData() { id = 1; name = "foo"; collegeName = "FooCollege"; } public static void main(String[] args) { Student student = new Student(); student.setData(); } } 

This is what happens when the same code is compiled to get a class file

 class Student { private int id; private String name; static String collegeName; void setData() { this.id = 1; this.name = "foo"; this.collegeName = "FooCollege"; // which will be again as Student.collegeName } public static void main(String[] args) { Student student = new Student(); student.setData(); } } 

Here, this represents the Student reference variable from the main method. Represents the reference variable to which the block is invoked.

Returning to the question, the main method creates a Test object and is called in its reference method1() . Thus, inside method1 this is nothing but the reference variable t created in the main method, and t is the local reference variable of the method. Now let's rewrite the code in the class file format

 public void method1(int x) { Test t = new Test(); this.x = 22; // or Test.x = 22; y = 44; // or this.y = 44; /* Test object inside method1 and main method are in two different locations. When we write this.y = 44; the y inside the main method object will be changed and not the one created inside method1. */ System.out.println("Test.x: " + Test.x); System.out.println("tx: " + tx); System.out.println("ty: " + ty); // means the y inside the object created inside method1 System.out.println("y: " + y); // means the y inside the object created inside main method } 
+2
source

in method1 you have two objects t and this (current object), and the line

 y = 44; // equivalent to this.y = 44 

sets the value of the current object, therefore

 this.y == 44; or y == 44; ty == 33; 
+1
source

the point you should understand is that y = 44 or this.y = 44 does not change ty, if you want to change ty, you can do this: ty = 44;

0
source

Source: https://habr.com/ru/post/1274832/


All Articles