Java Classes

Java Classes

A class is a description of a set of objects and may contain one or more constructors to create objects.
A class is defined by specifying instance variables, instance methods and through relationships with other classes.

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);   
 }  
}