Classes, methods, access control
The data, or variables, defined within a class are called INSTANCE variables. The code is contained within methods. Collectively, the methods and variables defined within a class are called members of the class.
A parameter is a variable defined by a method that receives a value when the method is called.
An argument is a value that is passed to a method when it is invoked.
Garbage collection only occurs sporadically (if at all) during the execution of your program. It will not occur simply because one or more objects exist that are no longer used.
package main;
public class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
boolean equalTo(Test o) {
if(o.a == a && o.b == b) return true;
else return false;
}
}
package main;
public class Main {
public static void main(String[] args) throws Exception {
Test ob1 = new Test(100, 22);
Test ob2 = new Test(100, 22);
Test ob3 = new Test(-1, -1);
System.out.println("ob1 == ob2: " + ob1.equalTo(ob2)); // true
System.out.println("ob1 == ob3: " + ob1.equalTo(ob3)); // false
}
}Pass by value principle.package main;
public class Test {
void meth(int i, int j) {
i *= 2;
j /= 2;
}
}package main;
public class Main {
public static void main(String[] args) throws Exception {
Test t = new Test();
int a = 15;
int b = 20;
System.out.println("a and b before call: " + a + " " + b); // 15 20
t.meth(a, b);
System.out.println("a and b after call: " + a + " " + b); // 15 20
}
}
Pass by reference principleWhen you pass an object to a method, the situation changes dramatically, because objectsare passed by what is affectively call-by-reference.
package main;
public class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}package main;
public class Main {
public static void main(String[] args) throws Exception {
Test t = new Test(15, 20);
System.out.println("t.a and t.b before call: " + t.a + " " + t.b); // 15 20
t.meth(t);
System.out.println("t.a and t.b after call: " + t.a + " " + t.b); // 30 10
}
}Remember When an object reference is passed to a method, the reference itself is passed by
use of call-by-value. However, since the value being passed refers to an object, the copy of
that value will still refer to the same object that its corresponding argument does.
package main;
public class Test {
int a;
Test(int i) {
a = i;
}
Test incrByTen() {
Test temp = new Test(a + 10);
return temp;
}
}package main;
public class Main {
public static void main(String[] args) throws Exception {
Test t = new Test(2);
Test t2;
t2 = t.incrByTen();
System.out.println("t: " + t.a); // 2
System.out.println("t2: " + t2.a); // 12
t2 = t2.incrByTen();
System.out.println("t2 after a second increase: " + t2.a); // 22
}
}Recursion.
package main;
public class Factorial {
int fact(int n) {
int result;
if (n == 0) return 1;
result = fact(n - 1) * n;
return result;
}
}When writing recursive methods, you must have an if statement somewhere to force the return
without the recursive call being executed. If you don't do this, once you call the method,
it will never return.
package main;
public class Test {
int values[];
Test(int i) {
values = new int[i];
}
void printArray(int i) {
if (i == 0) return;
else printArray(i - 1);
System.out.println("[" + (i - 1) + "]" + values[i - 1]);
}
}Access modifiersJava's access modifiers are public, private and protected.protected applies only when inheritance is involved, and it it accessible from both the sameand another package if of course class extends the class of protected variable.public. When a member of a class is modified by public, then that member can be accessed byany other code.private. When a member of a class is specified as private, then that member can only beaccessed by other members of its class. Now you can understand why main() has always beenpreceded by the public modifier. It is called by code that is outside the program - that is,by the Java run-time system.*When no access modifier is used, then by default the member of a class is public within itsown package, but cannot be accessed outside of its package.static. Instance variables declared as static are, essentially, global variables.Methods declared as static have several restrictions:* They can only directly call other static methods of their class.* They can only directly access static variables of their class.* They cannot refer to this or super in any way.final. Doing so prevents its contents from being modified, making it, essentially, a constant.This means that you must initialize a final field when it is declared. You can do this in oneof two ways: First, you can give it a value when it is declared. Second, you can assign it avalue within a constructor.default. Default is not written and accessible from the same package.
Комментарии
Отправить комментарий