Guest User

Untitled

a guest
Jul 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. public class HelloWorld {
  2.  
  3. public static int x = 0;
  4.  
  5. public static void main(String[] args) {
  6.  
  7. HelloWorld.x = 45;
  8.  
  9. System.out.println(HelloWorld.x);
  10. }
  11. }
  12.  
  13. this.x = 42; //correct
  14. this->x = 42; //will not compile as its not valid Java
  15.  
  16. public class Foo {
  17.  
  18. private int x;
  19.  
  20. public void setX(int x) {
  21. this.x = x;
  22. }
  23.  
  24. public int getX() {
  25. return this.x;
  26. }
  27. }
  28.  
  29. public class HelloWorld {
  30.  
  31. public static void main(String[] args)
  32. {
  33. Foo foo = new Foo();
  34. foo.setX(45);
  35.  
  36. System.out.println(foo.getX());
  37. }
  38.  
  39. }
  40.  
  41. public class HelloWorld {
  42.  
  43. public int x = 0; // note: now it's an instance attribute
  44.  
  45. public static void main(String[] args) {
  46.  
  47. HelloWorld hw = new HelloWorld();
  48.  
  49.  
  50. System.out.println(hw.x);
  51. }
  52.  
  53. public int getX() { return this.x; }
  54. public void setX(int x) { this.x = x; }
  55. }
  56.  
  57. public class Foo {
  58. private int x;
  59.  
  60. public void setX(int x) {
  61. this.x = x; //Here the keywork this is necessary!
  62. }
  63.  
  64. public int getX() {
  65. return x; //in this case the 'x' can only be instance variable
  66. }
  67. }
  68.  
  69. public class HelloWorld {
  70. public static void main(String[] args) {
  71.  
  72. Foo foo1 = new Foo();
  73. Foo foo2 = new Foo(3, 4, 5);
  74.  
  75. System.out.println("Foo1:n" + foo1);
  76. System.out.println("Foo2:n" + foo2);
  77. }
  78. }
  79.  
  80. class Foo {
  81. private int x, y, z;
  82.  
  83. public Foo() {
  84. this(-1, -1);
  85. }
  86.  
  87. public Foo(int x, int y) {
  88. this.x = x;
  89. this.y = y;
  90. }
  91.  
  92. public Foo(int x, int y, int z) {
  93. this(x, y);
  94. this.z = z;
  95. }
  96.  
  97. @Override
  98. public String toString() {
  99. return "x= " + x + "ny= " + y + "nz= " + z + "n";
  100. }
  101. }
  102.  
  103. objectName.variableName = newValue
  104.  
  105. class Test {
  106.  
  107. int x;
  108. int y;
  109.  
  110. public void Test(int x, int y) {
  111. this.x=x;
  112. this.y=y;
  113. }
  114.  
  115. public void setX(int x) {
  116. this.x=x;
  117. }
  118. }
Add Comment
Please, Sign In to add comment