Java class Keyword

Java Class Keyword


Class Keyword In Java
Each, and everything in Java comes under a class, anything outside a class will through an error.
Class is a user denied data type that combines member data, and member method. Class is the fundamental block of oops. The class supports inheritance that increases the reusability of code.
The class keyword is used to declare a class.
Note -
Class is a keyword that is used to create a class / tells the compiler that a new data type is created.
java.lang package contains a class whose name is class that is used to register the driver.
java.lang package contains a class whose name is object. Object class is used when type is unknown.
There’s one Class object for each class that is part of your program. That is, each time you write and compile a new class, a single Class object is also created (and stored, appropriately enough, in an identically named .class file). To make an object of that class, the Java Virtual Machine (JVM) that’s executing your program uses a subsystem called a class loader.
Object ob;
ob = 10;
ob = "Ram";
Syntax -
Class className{
   dataType variable;
   // .....
   method Declaration;
}
Now we will see some examples -
Example 1 - Now we will see what will happen if we try to code outside the boundary of class.
Output  -
Example 2 -Here is a complete program that uses the Box class - 
package javaLearnings;
import java.util.Scanner;

class Box { // class named Box
  double width;
  double height;
  double depth;
}


public class Main {
  public static void main(String args[]){

          Box mybox = new Box();
          double vol;
// assign values to mybox's instance variables
          mybox.width = 21;
          mybox.height = 10;
          mybox.depth = 20;
// compute volume of box
          vol = mybox.width * mybox.height * mybox.depth;
          System.out.println("Volume is " + vol);
          }
      }


 
Output -