Ticker

6/recent/ticker-posts

Java Classes & Objects

Class

A class is the blueprint from which individual objects are created. Class is a one which contains states (variables) and behaviour (methods) 

Syntax – 

    class <className>

    

            //code 

    

public class Lover 

            String name; //state 

            int age; //state 

            char sex; //state it takes only M or F 

            double height; //state 

            void takeToPark() //behaviour 

            

                    System.out.println("Take in a R-15"); 

            

            int spendMoney() //behaviour 

            

                    int money=5000; 

                    return money; 

            

}

 

Object 

An object is an instance of a class. The term ‘object’, however, refers to an actual instance of a class. Every object must belong to a class. 

Note - Objects have a lifespan but classes do not 

Object of a calss can be created using the new keyword and name of the class. 

    new <className>

public class Test 

        public static void main(String[] args) 

         

                    Lover l1 = new Lover(); 

                    Lover l2 = new Lover(); 

        

ALSO SEARCH:

"classes and objects in java example programs"

"difference between class and object in java"

"how to create object in java with example"

"what is object in java with example"

"class in java"

"java class example"

"object in java"