How do i change a variable of another class?

NoahKr

I have a variable in the class "MainActivity" with the name modeNr, it is protected so it should be accessible from within the package, however whenever I try to alter it from a class in the same package it gives the error: "Non-static field 'modeNr' cannot be referenced from a static context". I use the following line to alter the variable:

MainActivity.modeNr = 1;

Any ideas on what the problem is and how to correct it?

Muneeb Nasir

you are trying to change non-static member from static function. you need to make that varible static as well or need to create object of that class. lets suppose,

class Test {
 int node = 0;
 static int node1 =10; 

}

class changeNode {

public static void changeNode(){
 new Test().node = somevalue;
//or you need to make node static and change like this
Test.node1 = some value 

}
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I Change or Assign a Value to a Private Variable JTextField from another class?

From Dev

How do I change a variable from a different class in Java?

From Dev

how to change the value of a static variable from a static method of another class

From Dev

How do I change a scope variable with a service?

From Dev

How do I gain access to an objects variable from another class?

From Dev

Lua: How can i reach a class variable from another class?

From Dev

How do I change the class on click?

From Dev

XNA: How do I change a variable value from another class?

From Dev

How do I limit what classes can change the value of a field in another non related class

From Dev

How do I use a specific variable of an object that is in another class?

From Dev

How do I change the variable value in the inner class

From Dev

How do I call a variable in another method

From Dev

How do I Change or Assign a Value to a Private Variable JTextField from another class?

From Dev

How do I get some variable from another class in Java?

From Dev

How do I change a <span> class name?

From Dev

How do I change a variable from a different class in Java?

From Dev

How can I use variable from class in another class

From Dev

How do I use a variable from another class in Unreal Script?

From Dev

How do I take the value of one variable and set it equal to another variable in a class?

From Dev

Java - How do I get a variable generated from within a method in one class to another class?

From Dev

How do I reference a string in another class?

From Dev

How do I change one username to another?

From Dev

How do I change a Variable, in a Java Class

From Dev

How do I execute a class in another activity

From Dev

How do I pass a variable during class instantiate in one file to another?

From Dev

How do I change variable in a function from another file?

From Dev

How do I change a variable inside a variable?

From Dev

How do i change a variable that i have defined in a different class

From Dev

How do I set a value to another variable in a class using get/set?

Related Related

  1. 1

    How do I Change or Assign a Value to a Private Variable JTextField from another class?

  2. 2

    How do I change a variable from a different class in Java?

  3. 3

    how to change the value of a static variable from a static method of another class

  4. 4

    How do I change a scope variable with a service?

  5. 5

    How do I gain access to an objects variable from another class?

  6. 6

    Lua: How can i reach a class variable from another class?

  7. 7

    How do I change the class on click?

  8. 8

    XNA: How do I change a variable value from another class?

  9. 9

    How do I limit what classes can change the value of a field in another non related class

  10. 10

    How do I use a specific variable of an object that is in another class?

  11. 11

    How do I change the variable value in the inner class

  12. 12

    How do I call a variable in another method

  13. 13

    How do I Change or Assign a Value to a Private Variable JTextField from another class?

  14. 14

    How do I get some variable from another class in Java?

  15. 15

    How do I change a <span> class name?

  16. 16

    How do I change a variable from a different class in Java?

  17. 17

    How can I use variable from class in another class

  18. 18

    How do I use a variable from another class in Unreal Script?

  19. 19

    How do I take the value of one variable and set it equal to another variable in a class?

  20. 20

    Java - How do I get a variable generated from within a method in one class to another class?

  21. 21

    How do I reference a string in another class?

  22. 22

    How do I change one username to another?

  23. 23

    How do I change a Variable, in a Java Class

  24. 24

    How do I execute a class in another activity

  25. 25

    How do I pass a variable during class instantiate in one file to another?

  26. 26

    How do I change variable in a function from another file?

  27. 27

    How do I change a variable inside a variable?

  28. 28

    How do i change a variable that i have defined in a different class

  29. 29

    How do I set a value to another variable in a class using get/set?

HotTag

Archive