Java is an Object-oriented programming (OOP) in which programs are organized around data, or objects, rather than functions and logic.
An object can be defined as a data field that has unique attributes and behaviour.
In real world, an object represents an entity that can be distinctly identified. For example, a student, a desk, a cat, a car, and even a loan can all be viewed as objects.
All these objects have a state and a behaviour. If we consider a cat, then its state is - name, breed, color, and the behavior is - meowing, climbing, running, etc.
If we consider the real-world we can find many objects around us, Cars, Humans, etc. All these objects have a state and behavior.
If you compare the software object with a real world object, they have very similar characteristics.
Software objects also have a state and behavior. A software object's state is stored in data fields and behavior is shown via methods.
Every object has attributes which collectively represent its state.
So in software development, methods operate on the internal state of an object and the object-to-object communication is done via methods.
A class is a template or blueprint that defines an object's data and methods.
An object is an instance of a class.
A Java class uses variables to define data fields and methods to define behaviors.
To create a class, use the keyword class.
Example-1:
public class Cars {
// This is a class
}
Note:
Declare it as 'public' to make it accessible from outside of the class.
Cars is a class's name.
Each class has instances and methods.
The example-2:
Each class has instances and methods.
public class Cars {
// instances
// methods
}
Example-2a:
The class Cars has three instance variables. One constructor method and three other methods.
public class Cars{
// Instance variable
String make;
int year;
int speed;
// constructor method
public Cars(String m, int y){
make = m;
year = y;
speed = 0;
}
// other methods
// this method will return the make
public String getMake(){
return make;
}
// this method will return the speed
public int getSpeed(){
return speed;
}
// this method will return the year
public int getYear(){
return year;
}
}
A class can contain any of the following variable types.
Local variables: Variables defined inside methods, constructors, or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has been completed. In the above example, m and y are local variables for constructor Cars.
Instance variables: Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor, or block of that particular class. In the above example, make, year and speed are instance variables for class Cars.
Class variables: Class variables are variables declared within a class, outside any method, with the static keyword. There are no class variables in the above example. We will see it later.
A class can have any number of methods to access the value of various kinds of methods. In the above example, getMake(), getSpeed() and getYear() are methods.
As mentioned previously, a class provides a template for the objects. So basically an object is created from a class. In Java, the new keyword is used to create new objects.
There are three steps to create an object from a class:
Declaration: A variable declaration with a variable name with an object type.
Instantiation: The new keyword is used to create the object.
Initialization: The new keyword is followed by a call to a constructor. This call initializes the new object.
Example-3:
public class TestCars{
public static void main(String []args){
// Following statement would create an object
Cars myCar = new Cars( "Iriz", 2016);
}
}
In the above example, the statement
Cars myCar = new Cars( "Iriz", 2016);
will create an object myCar from class Cars and invoke the constructor Cars (Example-2a) and pass two parameters; a string Iriz and an integer 2016 to variables make and year.
Example-4:
This program shows how to call the instances of the class using '.' dot operator. As you can see in this program, variable make and year are declared in class Cars.
public class TestCars{
public static void main(String []args){
// Following statement would create an object myCar
Cars myCar = new Cars( "Iriz", 2016);
System.out.println(myCar.make); //calling instance make from class Cars
System.out.println(myCar.year); //calling instance year from class Cars
}
}