Objects and Classes in Java

Object : In Java, objects are essentially a block of memory that contains space to store all the instance variables.

Creating Objects : Creating an object is also referred to as instantiating an object.
In java, objects are created by using the new operator. The new operator creates an object of the specified class and returns a reference to that object.

Test t1;  // declare the object
t1=new Test(); // instantiate the object

First statement declares a variable to hold the object reference and the second statement assigns the object reference to the variable. The variable t1 is now an object of the Test class.

Class : A class is a user-defined data type with a template that serves to define its properties. A class is essentially a description of how to make an object that contains fields and methods.

Creating class :

class classname                      
   field declaration;
   methods declaration;

Application of classes and objects :
class Home                          // Declaration of class
 int l, w;
void getdata(int x, int y)
int homearea()
 int area=l*w;
class Test                           // Declaration of class with main method 
public static void main(string arg[])
int a1, a2;
Home h1=new Home();                  // creating 1st object
Home h2=new Home();                  // creating 2nd object
System.out.println("Area1 is ="+a1);
System.out.println("Area2 is ="+a2);

Output :
Area1 is = 500
Area2 is = 300

Accessing Class Members : Now we have created objects in before program, each containing its own set of variables, we should assign values to these variables in order to use them in our program. all variables must be assigned values before they are used. We are outside the class, we cannot access the instance variables and the methods directly. To do this, we must use the concerned object and the dot operator(.)


Here object-name is the name of the object, variable-name is the name of the instance variable inside the object that we want to access, method name is the method that we want to call and parameter-list is a comma-separated list of actual values that must match in type and number with the parameter list of the method name declared in the class.
The instance variables of the Home class may be accessed and assigned values are given as follows :

In second case, the method getdata can be used to call Home objects to set all values of l and w.
Home h2=new Home();  // creating an object
h2.getdata(15,20);   // calling the method using the object 

Recommended Posts:

    1. Features of Java
    2. Java Program Structure
    3. Command Line Arguments in Java
    4. Difference between java and c++
    5. Data Types in Java with example
    6. Scope of Variables in Java
    7. Operators in Java
    8. Looping Statement in Java
    9. Objects and Classes in Java
    10. Constructors in Java with example
    11. Method Overloading in Java with example
    12. Method Overriding in Java with example
    13. Static Members in Java with Program
    14. Abstract Class in Java with Program
    15. final, finally and finalize in Java
    16. Dynamic Method Dispatch in Java | Runtime Polymorphism
    17. Nesting of Methods in java
    18. Inheritance in Java with example
    19. Java Access Modifiers – Public, Private, Protected, Friendly
    20. Array in Java
    21. Strings in Java | String Methods
    22. StringBuffer class in Java with Program
    23. Vector in Java | Vector Methods
    24. Wrapper Classes in Java With Program
    25. Autoboxing and Unboxing in Java With Program
    26. enum in Java with Program
    27. Java Annotation
    28. Interface in Java with Program
    29. Difference between Class and Interface in Java
    30. Implementing Interfaces in Java
    31. Java Program to implement Multiple Inheritance using Interface
    32. Difference Between Multithreading and Multitasking in Java
    33. Life Cycle of Thread in Java with Diagram
    34. Synchronization of threads in Java
    35. Java Thread Priority
    36. Java Program to Implement Multithreading using Runnable Interface
    37. Inter-thread Communication in Java
    38. Suspend, Resume and Stop Thread in Java
    39. Types of Error in Java
    40. Exceptions in Java
    41. Exception Handling in Java with examples
    42. Java Package
    43. Static import in Java
    44. Design a package to contain class Student and package contain interface Sports