Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HelloWorld {
- public static int x = 0;
- public static void main(String[] args) {
- HelloWorld.x = 45;
- System.out.println(HelloWorld.x);
- }
- }
- this.x = 42; //correct
- this->x = 42; //will not compile as its not valid Java
- public class Foo {
- private int x;
- public void setX(int x) {
- this.x = x;
- }
- public int getX() {
- return this.x;
- }
- }
- public class HelloWorld {
- public static void main(String[] args)
- {
- Foo foo = new Foo();
- foo.setX(45);
- System.out.println(foo.getX());
- }
- }
- public class HelloWorld {
- public int x = 0; // note: now it's an instance attribute
- public static void main(String[] args) {
- HelloWorld hw = new HelloWorld();
- System.out.println(hw.x);
- }
- public int getX() { return this.x; }
- public void setX(int x) { this.x = x; }
- }
- public class Foo {
- private int x;
- public void setX(int x) {
- this.x = x; //Here the keywork this is necessary!
- }
- public int getX() {
- return x; //in this case the 'x' can only be instance variable
- }
- }
- public class HelloWorld {
- public static void main(String[] args) {
- Foo foo1 = new Foo();
- Foo foo2 = new Foo(3, 4, 5);
- System.out.println("Foo1:n" + foo1);
- System.out.println("Foo2:n" + foo2);
- }
- }
- class Foo {
- private int x, y, z;
- public Foo() {
- this(-1, -1);
- }
- public Foo(int x, int y) {
- this.x = x;
- this.y = y;
- }
- public Foo(int x, int y, int z) {
- this(x, y);
- this.z = z;
- }
- @Override
- public String toString() {
- return "x= " + x + "ny= " + y + "nz= " + z + "n";
- }
- }
- objectName.variableName = newValue
- class Test {
- int x;
- int y;
- public void Test(int x, int y) {
- this.x=x;
- this.y=y;
- }
- public void setX(int x) {
- this.x=x;
- }
- }
Add Comment
Please, Sign In to add comment