Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Java Class
Java Object
Java Object Array
Java Nesting of Classes
Java Methods
Java Method Overloading
Java Constructor
Java Constructor Overloading
Java This Keyword
Java Garbage Collection
Java Static Keyword
Java Arrays
Java String
Java StringBuffer
Java String vs StringBuffer
Java Vector Class
Java Vector vs Array
Java Wrapper Classes

Inheritance

Java Inheritance
Java Single Inheritance
Java Super Keyword
Java Multiple Inheritance
Java Hierarchical Inheritance
Java Multilevel Inheritance
Java Method Overriding
Java Abstract Classes,Methods
Java Final Variables, Methods
Java Dynamic Dispatch Methods
Java Visibility Controls

Interfaces

Java Interface
Java Class vs Interface
Java Multiple Inheritance
Java Nested Interface

Packages

Java Packages
Java Built in packages
Java Package Access Specifiers

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Objects


Java Object Structure

An object is an instance of class. It has its own states and behaviour which is called as properties of object.

Ex- consider a class human which having states color,name,place,contact no etc. and the behaviour is of human is walking(), running(), teaching() etc.

The class defines only logical structure but object contains the actual data is to be stored. We can create any number of objects of a class. But every object stores the different data.

How to create an object?

  1. Declare a variable of type class in the main method. This variable does not define an object, But it simply refers to an object.
    Ex- Human h1;
  2. Assign a reference to the object by using new keyword with class name and empty brackets.
    Ex- h1 = new Human();
    The new operator dynamically allocates memory for an object and returns reference to the variable.

Example

class Human
{
    public static void main(String args[])
    {
        Human h1;
        h1 =  new  Human();

        // or you can write in single statement as
        Human h2 = new Human();
    }
}
	

How to access class members using object?

Once the object of class is created, the dot (.) operator is used to access the methods and fields of a class or from outside the class.

Example – accessing the data from same class using object

class City
{
    String CityName;
    int villages;
    float area; 
    void display()
    {
      System.out.println("City name : "+CityName);
	System.out.println("Total Villages: "+villages);
	System.out.println("Area: "+area);
    }

    public static void main(String args[])
    {
	City mycity;
	mycity = new City();

	// initializing class variables using object;
	mycity.CityName = " ";
	mycity.villages = ;
	mycity.area = ;

	mycity.display(); // calling method using object
    }
}

Output

City name :
Total Villages :
Area :

In above example we have initialized the instance fields using object and called one member function using object mycity


How to access class data from another class?

Example – same example using two classes

class City
{
    String CityName;
    int villages;
    float area; 
    void display()
    {
	 System.out.println("City name : "+CityName);
	 System.out.println("Total Villages: "+villages);
	 System.out.println("Area: "+area);
    }
}

class mainMethod
{
     public static void main(String args[])
     {
	 City mycity;
	 mycity = new City();

	 // initializing class variables using object;
	 mycity.CityName = " ";
	 mycity.villages = ;
	 mycity.area = ;

	 mycity.display();
      }
}
	

Output

City name :
Total Villages :
Area :
Note : while compiling the program make sure you are compiling using the class name having main() method.
Ex- java mainMethod

In above example we have two classes in which City class contains all the data members and the second class contains only main method for creating an object.

You can define any numbers of classes, but while calling their methods we must create their objects.


How to create multiple objects of a class?

To create multiple objects of a class, follow above same procedure for declaration but give them a different name just like a normal variable.

<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo