Java Classes

Java Classes

A class is a description of a set of objects and may contain one or more constructors to create objects.
All objects have the same attributes class and do the same operations, but their condition is different and have their own identity.
A class is defined by specifying instance variables, instance methods and through relationships with other classes.
Instance variables defining the state of a class.
Instance methods can change the status of a class.

A Java class can be composed of:

  • Variables
  • Constructors
  • Methods
  • Classes

Declare class example

public class ClassName {
// variables
// constructors
// methods
// classes
}

Class example

public class People{  
    int id=1; //instance variable
    String name="Nicole"; //instance variable
 
 public static void main(String args[]){ 
     People p=new People(); //create object People  
     System.out.println("Id: "+p.id+" Name: "+p.name);   
 }  
}