Saturday, July 20, 2019

What is Java Class?



What is Java Class?

A class is an entity that determines how an object will behave and what the object will contain. In other words, it is a blueprint or a set of instruction to build a specific type of object.

Syntax :

class <class_name>{
field;
method;
}

So a class is the blueprint from which individual objects are created.




The following Bike class is one possible implementation of a Bike:

class Bike
{
    int speed = 0;
    int gear = 1;
    void changeGear(int newValue)
               {
         gear = newValue;
    }
    void speedUp(int increment)
               {
         speed = speed + increment;  
    }
    void applyBrakes(int decrement)
               {
         speed = speed - decrement;
    }
    void printStates()
               {
         System.out.println(" speed:" + speed + " gear:" + gear);
    }
}

The fields speed, and gear represent the object's state, and the methods (applyBrakes, changeGear, speedUp , printStates etc.) define its interaction with the outside world.

You may have noticed that the Bike class does not contain a main method. That's because it's not a complete application; it's just the blueprint for Bike that might be used in an application.

Here's a BikeDemo class that creates two separate Bike objects and invokes their methods:

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

        // Create two different Bike objects
        Bike bike1 = new Bike();
        Bike bike2 = new Bike();

        // Invoke methods on those objects
        bike1.speedUp(10);
        bike1.changeGear(2);
        bike1.printStates();

        bike2.speedUp(10);
        bike2.changeGear(2);
        bike2.speedUp(10);
        bike2.changeGear(3);
        bike2.printStates();
    }
}

The output of this test prints the ending pedal cadence, speed, and gear for the two Bikes:

speed:10 gear:2
speed:20 gear:3

Class naming standards:
  1. Class name should be a noun
  2. Keyword not be a class name
  3. Class name does not start with digits but you can put it after the class name
  4. If class name contains more than two words, then each word’s first later should be capital
  5. If you want to space between class name that does not allow instead of you can use $ and _ symbol.
Important about class :
  1. Every java program has at least one public class
  2. The file name of your java program will be the class name as declared public
  3. There are FIVE members in a class.
  • Member Variables (States)
  • Methods (Behaviors)
  • Constructor
  • Blocks (Instance/Static Blocks )
  • Inner Classes.
      4. Types of classes in java
  • Parent class
  • Child class
  • Inner class
  • Abstract class
  • Wrapper class
      5. class means nothing but bunch of fields, methods and constants, these methods are in the form of instructions are stored in Method Area .

public class Animal
{
int field;
static int constant;
public void fun()
 {
int localVariable=10;
Animal obj=new Animal();
  }
}

In the example, Animal.class will go to Method Area, that means field, constant and method fun are allocated in Method Area.
When the execution is started, The object created by new Animal() will go to Heap but obj is just a object reference which goes to stack and holds the address of object which is present in the Heap





1 comment:

  1. Please write something for the article.How is helpful for you?

    ReplyDelete