Why a static method cannot directly access a value changed by a non-stationary method

public class JustPractice { public int points=0; public static void main(String args[]) { JustPractice ha = new JustPractice(); ha.end(); happy(); } public void end() { this.points=100; System.out.println(points); } public static void happy() { JustPractice object = new JustPractice(); System.out.println(object.points); return; } } 

The above shows:

100
0

whereas it should be displayed:

100
100

+5
source share
6 answers

You are viewing two different instances of your class.

Each instance gets its own copy of the instance fields, and they are completely independent of each other.

 JustPractice ha = new JustPractice(); ha.end(); // this one has "100" JustPractice object = new JustPractice(); // this one has "0" System.out.println(object.points); 

The static method can only access the fields of the ha instance if you provide it with ha as a parameter.

+6
source

Make points static . Then you got what you want.

 public static int points=0; 

make points static will contain only one variable for all instances of your class.

Otherwise, each initialization will create a separate separate variable and assign a value of 0

+2
source

due to the fact that the method creates a new object, then it will have another copy, and the reference object will have an independent copy on the class object, remember the base java?

and if u makes int as a static object, then it will give your result what you want and that you are requesting a simple example from yours.

 public static int points = 0; public void end() { this.points = 100; System.out.println(points); } public static void happy() { CheckingClass object = new CheckingClass(); System.out.println(object.points); return; } public static void main(String args[]) { CheckingClass ha = new CheckingClass(); ha.end(); happy(); } 

hope it will be useful

+2
source

A static method can access a non-stationary object, but only if it is passed to the method. Given that the static method in question is the method in which the program starts, you have no way to pass anything to it.

What he cannot do within the non-static class is access to any of the instance members or functions. The problem is not that you create ha from a static method, but that you are accessing member members, which is a member of an instance that belongs to every instance of your class.

To fix this, you can either pass the points to the JustPractice (ha) instance that you create and return one of the JustPractice methods the final value, or you can make the points end () and happy (), in which case everyone can work happily together.

0
source

it is displayed as 100 0 instead of 100 100 because you are creating a new instance of the JustPractice class in your happy () method instead of passing the ie ha link to the existing instance.

the new newly created instance shows the default value specified in your class, i.e. 0.

0
source

You set the value of the points instance variable in the end.and method for the object in the happy method; you do not initialize the points instance instance variable.

Thus, it gets the default value of 0.

0
source

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


All Articles