Friday 10, February 2012
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    Forum    |    About Us    |    Contact Us

Java Constructors

A java constructor has the same name as the name of the class to which it belongs. Constructor's syntax does not include a return type, since constructors never return a value. Constructors may include parameters of various types. When the constructor is invoked using the new operator, the types must match those that are specified in the constructor definition. Java provides a default constructor which takes no arguments and performs no special actions or initializations, when no explicit constructors are provided. The only action taken by the implicit default constructor is to call the superclass constructor using the super() call. Constructor arguments provide you with a way to provide parameters for the initialization of an object.

Example:
public class MainClass {
  double radius;

  MainClass() {

  }
 
  MainClass(double theRadius) {
    radius = theRadius;

  }

}

Example:
Multiple Constructors
class Sphere {
  int radius = 0;

  Sphere() {
    radius = 1;
  }

  Sphere(int radius) {
    this.radius = radius;
  }
}

Java Multiple Constructors
We can always use the default constructor of a class if it meets our needs. It is automatically there when a class is declared. Here are some examples to recap:

Circle redCircle = new Circle();
Circle blueCircle = new Circle();
Employee mechanic = new Employee();
Employee welder = new Employee();

As we saw last lesson, we can overwrite our default constructor with our own:

public class Circle{
        int xcenter;
        int ycenter;
        int radius;
        // our overwritten constructor
        public Circle(){
               radius = 1;
        }
}

When the above constructor is used the radius has a value of 1. If we wanted to make a Constructor that allowed us to give our own value to its radius , for example , we could supply a constructor as follows:

public Circle(int _radius){
        radius = _radius;
}

And it would be called like:
Circle smallCircle = new Circle(2); // radius = 2
or
Circle bigCircle = new Circle(569); // radius = 569
We could go even further and allow the users of our class to set every variable of our Circle class by supplying a further Constructor with 3 arguments :

public Circle(int _x, int _y, int _radius){
        xcenter = _x;
        ycenter = _y ;
        radius = _radius;
}

Let us see what our class looks like with our added constructor now :

public class Circle{
        int xcenter;
        int ycenter;
        int radius;
        // our overwritten default constructor
        public Circle(){
               radius = 1;
        }
        // Constructor with 1 argument
        public Circle(int _radius){
               radius = _radius;
        }
        // our constructor with 3 arguments
        public Circle(int _x, int _y, int _radius){
               xcenter = _x;
               ycenter = _y ;
               radius = _radius;
        }
}

So now we have 3 choices when we instantiate our Circle class. We can go :
Circle defaultCircle = new Circle(); // radius is 1
or :
Circle uniCircle = new Circle(27); // radius is 27
or :
Circle customCircle = new Circle(100, 75, 14);
This custom Circle will be created at position 100:75 , and have a radius of 14.
Of course, this Circle class would need set and get access methods to be written, especially if you wanted to move them around or change them someway.


 
     
   
Java Language Tutorial
 
  What is JAVA
What is JAVA
  Java an Object Oriented Language
Java an Object Oriented Language
  Java Keywords
Java Keywords
  Data Types
Data Types
  Java Operators
Java Operators
  Control Structure
Control Structure
  Arrays
Arrays
  Java Classes
Java Classes
  Java Constructors
Java Constructors
  Java Methods
Java Methods
  Java Method Overloading
Java Method Overloading
  Java Method Overriding
Java Method Overriding
  Java Nested Classes
Java Nested Classes
  Inheritance in Java
Inheritance in Java
  Interfaces in Java
Interfaces in Java
  Polymorphism in Java
Polymorphism in Java
  Exception Handling in Java
Exception Handling in Java
  Catching Exceptions
Catching Exceptions
  Embedding information in an exception object
Embedding information in an exception object
  The throws clause
The throws clause
  Checked and unchecked exceptions
Checked and unchecked exceptions
  The finally clause
The finally clause
  Applets in Java
Applets in Java
  Running an Applet
Running an Applet
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
  ASP Tutorial
ASP Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download Communication Network Analysis  Ebook Communication Network Analysis
   
Download Art of Programming Contest  Ebook Art of Programming Contest
   
Download Optimizing C++ Ebook Optimizing C++
   
Download Open Source Security Tools: Practical Guide to Security Applications  Ebook Open Source Security Tools: Practical Guide to Security Applications
   
Download The eBay Starters Kit  Ebook The eBay Starters Kit
   
 
Studiesinn.com © 2012 All Rights Reserved.
Website Designed & Developed by TechXprtz