What are Java classes?

Java 8Object Oriented ProgrammingProgramming

A class in Java is a user-defined datatype, a blueprint, a classification, that describes the behavior/state that the object of its type support.

Example

public class Dog {
   String breed;
   int age;
   String color;
   void barking() {
   }
   void hungry() {
   }
   void sleeping() {
   }
}

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 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.

raja
Published on 05-Jan-2018 11:12:47
Advertisements