BankAccount example. You don't
want user to change the name. And saving
can only be changed through withdraw method and
deposit method. So both name and saving
are declared private .private and public To declare a private member, use the private keyword in
its declaration. The following class contains one private member
variable and one private method:
class Alpha {
private int iamprivate;
private void privateMethod() {
System.out.println("privateMethod");
}
}
iamprivate variable and can't invoke privateMethod.
For example, the Beta class defined here:
class Beta {
void accessMethod() {
Alpha a = new Alpha();
a.iamprivate = 10; // illegal
a.privateMethod(); // illegal
}
}
Beta class cannot access the iamprivate
variable or invoke privateMethod on an object of type
Alpha because Beta is not of type Alpha
.andBeta.java:9: Variable iamprivate in class Alpha not
accessible from class Beta.
a.iamprivate = 10; // illegal
^
1 error
The following code is legalBeta.java:12: No method matching privateMethod()
found in class Alpha.
a.privateMethod(); // illegal
1 error
class Alpha {
private int iamprivate;
boolean isEqualTo(Alpha anotherAlpha) {
if (this.iamprivate == anotherAlpha.iamprivate)
return true;
else
return false;
}
}
Objects of the same type have access to one another's private
members. This is because access restrictions apply at the class or
type level (all instances of a class) rather than at the object
level (this particular instance of a class).
public class Alpha {
public int iampublic;
public void publicMethod() {
System.out.println("publicMethod");
}
}
and
public class Beta {
void accessMethod() {
Alpha a = new Alpha();
a.iampublic = 10; // legal
a.publicMethod(); // legal
}
}
aFloat inMyClass:
class MyClass {
float aFloat;
}
you declare an instance variable.static modifier). The runtime
system allocates class variables once per class regardless of the
number of instances created of that class.Student class with an instance
variable name
public class Student {
private String name;
private int numOfStudents;
public Student(String name) {
this.name = name;
// increase the number of students by 1
numOfStudents++;
}
public String getName() {
return name;
}
public int getNumOfStudents() {
return numOfStudents;
}
public static void main(String[] args) {
Student Amy, Ben, Charles;
Amy = new Student("Amy");
Ben = new Student("Ben");
Charles = new Student("Charles");
System.out.println(Charles.getNumOfStudents());
}
}
numOfStudents is assigned to be 0
implicitly since we don't explicitly assign the value to it.Student, all its variables
will initiate according to the constructor, so the variable numOfStudents is 0++ = 1 after you create
the object. private int numOfStudents;to
public int getNumOfStudents();
private static int numOfStudents;After the change
public static int getNumOfStudents();
numOfStudents is a class memeber and
getNumOfStudents() is a class method.
public class Student {
private String name;
private static int numOfStudents;
public Student(String name) {
this.name = name;
// increase the number of students by 1
numOfStudents++;
}
public String getName() {
return name;
}
public static int getNumOfStudents() {
return numOfStudents;
}
public static void main(String[] args) {
Student Amy, Ben, Charles;
Amy = new Student("Amy");
Ben = new Student("Ben");
Charles = new Student("Charles");
System.out.println(Student.getNumOfStudents());
}
}
static
static keyword.private static int numOfStudents; and
public static int getNumOfStudents() numOfStudents only initiate once. The variable(
method) are shared by all the objects of the same class.classname.variableExample :
classname.method(argruments)
Students.getNumOfStudents() . charles.getNumOfStudents().
class Classroom {
static final int MAX_CAPACITY = 10;
boolean full = false;
}
public class MyMath {
// define a constant PI
public static final double PI = 3.14;
public static double areaOfCircle(double radius) {
return PI*radius*radius;
}
}
Here is the test program
public class MyMathTest {
public static void main(String[] args) {
double radius = 5;
System.out.println("PI is " + MyMath.PI);
System.out.println("The area of circle with radius = " + radius +
" is " + MyMath.areaOfCircle(radius));
}
}