Java class is nothing but a template for object you are going to create or it’s a blue print by using this we create an object. In simple word we can say it’s a specification or a pattern which we define and every object we define will follow that pattern.
What does Java Class consist of :- When we create class in java the first step is keyword class and then name of the class or identifier we can say.
- Next is class body which starts with curly braces {} and between this all things related with that class means their property and method will come here.
What are the members of Class :
When we create a class its totally incomplete without defining any member of this class same like we can understand one family is incomplete if they have no members.
Field :
Field is nothing but the property of the class or object which we are going to create .for example if we are creating a class called computer then they have property like model, mem_size, hd_size, os_type etc Method :
Method is nothing but the operation that an object can perform it define the behavior of object how an object can interact with outside world .startMethod (), shutdownMethod (). Access Level of members: Access level is nothing but where we can use that members of the class.
Each field and method has an access level:
- private: accessible only in this class
- package or default: accessible only in this package
- protected: accessible only in this package and in all subclasses of this class
- public: accessible everywhere this class is available
Example :
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
}
type methodname2(parameter-list) {
// body of method
}
// ...
type methodnameN(parameter-list) {
// body of method
}
}
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. In most classes, the instance variables are acted
upon and accessed by the methods defined for that class. Thus, it is the methods that
determine how a class’ data can be used.
As stated earlier, each object has its own copies of the instance variables. This means that if you have two Box objects, each has its own copy of depth, width, and height. It is important to understand that changes to the instance variables of one object have no effect on the instance variables of another.
Example :
/* A program that uses the Box class.
Call this file BoxDemo.java
*/
class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
You should call the file that contains this program
BoxDemo.java, because the main( ) method is in the
class called BoxDemo, not the class called Box. When
you compile this program, you will find that two
.class files have been created, one for Box and one
for BoxDemo. The Java compiler automatically puts
each class into its own .class file. It is not
necessary for both the Box and the BoxDemo class to
actually be in the same source file. You could put
each class in its own file, called Box.java and
BoxDemo.java, respectively.
To run this program, you must execute BoxDemo.class.
When you do, you will see the following output:
Volume is 3000.0