Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Java - Constructor in java 
 

In this section you will learn how to implement the constructor feature in a class.

 

Java - Constructor in java

                         

Every class has at least one it's own constructort. Constructor creates a instance for the class. Constructor initiates (initialize) something related to the class's methods. Constructor is the method which name is same to the class. But there are many difference between the method (function) and the Constructor.

In this example we will see that how to to implement the constructor feature in a class. This program is using two classes. First class is another and second is the main class which name is Construct. In the Construct class two objects (a and b) are created by using the overloaded another Constructor by passing different arguments and calculated the are of the different rectangle by passing different values for the another constructor.

Here is the code of the program : 

class another{
  
int x,y;
  another(
int a, int b){
    x = a;
    y = b;
  }
  another(){
  }
 
 int area(){
    int ar = x*y;
 
   return(ar);
  }
}
public class Construct{
 
  public static void main(String[] args)
   {
     another b = 
new another();
     b.x =
 2;
     b.y =
 3;
     System.out.println("Area of rectangle : " + b.area());
     System.out.println("Value of y in another class : " + b.y);
     another a = 
new another(1,1);
     System.out.println("Area of rectangle : " + a.area());
     System.out.println("Value of x in another class : " + a.x);
   }
}

Output of the program : 

C:\chandan>javac Construct.java

C:\chandan>java Construct
Area of rectangle : 6
Value of x in another class : 3
Area of rectangle : 1
Value of x in another class : 1

Download this example.

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

5 comments so far (
post your own) View All Comments Latest 10 Comments:

ERROR IN OUTPUT
LINE NO 2

Posted by rahul sharma on Wednesday, 12.17.08 @ 01:09am | #82850

hey pls explain constructor concept in detail. Thought its role is simple hope it plays a major role in declaring variables

Posted by murali vihar on Saturday, 11.15.08 @ 11:12am | #81704

there is a small print mistake

Value of x in another class : 3

it should be

Value of Y in another class : 3

Posted by raviraj on Wednesday, 10.15.08 @ 12:25pm | #81093

how is work in constructor.

Posted by vijayanandp on Wednesday, 09.24.08 @ 17:20pm | #80661

This is not sufficient example could u give more explanations then only it is possible to learn constructor

Posted by narsi on Saturday, 09.20.08 @ 11:51am | #80561

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.