SunQuest
 
       Java
  Home arrow Java arrow Page 4 - Programming with Constructors in Java
IBM developerWorks
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
VeriSign Whitepapers 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Programming with Constructors in Java
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2006-07-17

    Table of Contents:
  • Programming with Constructors in Java
  • The default constructor in Java: demo and explanation
  • Constructors with parameters in Java: demo and explanation
  • Overloading constructors in Java: definition
  • Overloading constructors in Java: demo

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Programming with Constructors in Java - Overloading constructors in Java: definition


    (Page 4 of 5 )

    In my previous articles, I already covered the concept of overloading of methods (or method overloading).  If this is a new concept for you, I suggest you refer to my previous articles.

    In the previous section, I used a statement something like the following:

            MyCalc obj1 = new MyCalc(10,20);

    By now you understand that the code snippet above creates an instance/object, calls the constructor which has two parameters, and finally assigns the reference to “obj1.”  Let me modify the same like this:

            MyCalc obj1 = new MyCalc();

    If you execute your program with the above statement, without having a default constructor (covered in first section of this article), it generates a compilation error.  That means it could not find any constructor without parameters, and thus it cannot create an instance. 

    Let me rewrite the class to something like the following:

      public class MyCalc {

        int x;
        int y;

        public void setValues(int m, int n) {
            x = m;
            y = n;
        }

        public int getSum() {
            int z;
            z = x + y;
            return z;
        }

      }

    Now, the above class doesn’t have any constructor. With it, if you try to execute the following statement again, it will not result in any error.

            MyCalc obj1 = new MyCalc();

    How is this possible?  I didn’t include any default constructor in the above class (nor any other constructor with parameters).  That is simply a trick.  If a class is not equipped with any constructor, JVM creates one for us (internally) automatically. 

    Now, let me consider the case of the following two statements:

            MyCalc obj1 = new MyCalc();
            MyCalc obj2 = new MyCalc(10,20);

    Within the above two statements, I am trying to create “obj1,” which tries to call the default constructor (or the constructor which doesn’t have any parameters).  I am also trying to create “obj2,” which tries to call the constructor having two parameters.

    Can I include two constructors in the same class? Yes. You can have any number of constructors within a single class, but with a difference in parameters. This is called “constructor overloading” (or overloading of constructors).

    The next section gives you the full code to demonstrate “constructor overloading.”

    More Java Articles
    More By Jagadish Chaterjee


       · Hello guys. Now you can code with constructors in Java using NetBeans IDE with this...
     

    JAVA ARTICLES

    - Deploying Multiple Java Applets as One
    - Deploying Java Applets
    - Understanding Deployment Frameworks
    - Database Programming in Java Using JDBC
    - Extension Interfaces and SAX
    - Entities, Handlers and SAX
    - Advanced SAX
    - Conversions and Java Print Streams
    - Formatters and Java Print Streams
    - Java Print Streams
    - Wildcards, Arrays, and Generics in Java
    - Wildcards and Generic Methods in Java
    - Finishing the Project: Java Web Development ...
    - Generics and Limitations in Java
    - Getting Started with Java Web Development in...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway