Java is an object-oriented language which supports OOP features very well.
Here are some fundamental concepts of Java, which are:
- Message Passing
An object in Java is like a combination of data and procedures. Every object has a state and behavior.
Example: A cow has states – color, name, and behavior such as eating. The state of an object is stored in variables and function displays the behavior of objects.
A class is like a context which is used to create objects and defines objects state and behavior.
Objects in Java
Let us go to deeper about what are objects. Suppose Objects according to the real world, we will find a lot of objects around us, cars, dogs, humans, etc. All these objects have their own state and behavior.
Let us consider a dog, its state is – name and the behavior is – barking. If we compare the software objects with real-world objects, we will see a no. of similar characteristics.
Software objects are the same as real-world objects and also have a state and behavior. A software object’s state is stored in fields and behavior is shown via methods.
That’s why in software development, methods operate on the internal state of an object and the object-to-object communication is also done via methods.
Classes in Java
A class is just like a blueprint from which objects are created. A class contains 3 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 completed.
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 blocks of that particular class.
Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.
A class can have any number of methods to access the value of various kinds of methods. In the above example, barking(), hungry() and sleeping() are methods.
Following are some of the important topics that need to be discussed when looking into classes of the Java Language.