Adv Java
Programming

1. What Is the Java Technology 2. Points to keep in mind for pyramid. 3. Java Pyramid Program on type 1 4. Java Pyramid Program on type 2 5. Java Pyramid Programs on Type 3 6. Java Pyramid Programs on type 4 7. Java Pyramid Programs on type 5 8. Classes and Objects 9. Recursive functions 10. Constructors in Java 11. Method Overloading 12. Constructor Overloading 13. Inheritance in Java 14. Interface in Java 15. Method Overriding and Hiding Method 16. The final keyword 17. Exception Handling 18. Properties, System & Runtime Class in Java 19. Building Java GUI 20. Layout Managers 21. Menu Bar 22. Applets 23. Threading 24. Java Netwoking 25. Programs for Science and Engineer 26. Prg to demonstrate pre and post increment 27. Prg to convert Farenheit to Celsius temperature 28. Prg to convert kilogram to different units 29. Prg to calculate surface area and volume of sphere 30. Prg to calculate frequency 31. Prg to determine co-ordinate points 32. Prg to determine Equilateral, Isosceles or Scalene triangle 33. Prg to demonstrate conditional operator 34. Prg to calculate Final and initial interest amount 35. Prg to separate entered number 36. Prg to examine while loops and conditions 37. Prg to calculate number of year 38. Prg to find Armstrong number or not 39. Prg to find all Armstrong number 40. Prg to find limited Armstrong number 41. Prg to find Armstrong number using user defined function 42. Prg to find frequency of student 43. Prg to find maximum and minimum number 44. Prg to print number triangle 45. Prg to print vertical number triangle 46. Prg to print number triangle 47. Prg to print binary right angle triangle 48. Prg to print * triangle 49. Prg to to print rhombus 50. Prg to print entered number triangle 51. Prg to to find nCr and nPr 52. Prg to enter an element in array 53. Prg to sort number using array 54. Prg to find the position of element in array 55. Prg to find the position of element in array using boolean flag 56. Prg to add one extra element in an array 57. Prg to find the equation of line 58. Prg to find the common element from two array 59. Prg to find the highest scorer 60. Prg to create a square of number 61. Prg to create Pascal triangle 62. Prg to subtraction, multiplication and division using methods and classes 63. Prg to determine salary grade 64. Prg for complex number 65. Prg to calculate area 66. Prg for Greatest Common Divisior 67. Prg for displaying studenta mark sheet 68. Prg for demonstrating inheritance 69. Prg to calculate area of triangle 70. Prg for calculating volume of sphere or hemisphere 71. Prg for demonstrate classes 72. Prg for employee details 73. Prg for demonstrating super and base class 74. Prg for Bank Account 75. Prg for professor and student details 76. Prg for Addition, Multiplication and finding Transpose of two matrix 77. Prg for counting number of words in a string 78. Prg to count number of vowels and consonants in a string 79. Prg to fine number of upper case, lower case, white spaces and digit in a string 80. Prg to fine number of upper case, lower case, white spaces and digit in a string without inbuilt function 81. Prg to arrange a string in alphabetic order 82. Prg to count the number of character occurrence in a string 83. Prg to find the string length and character position 84. Prg for converting first character of word into upper case 85. Prg for demonstrating vectors for manipulating records 86. Prg for manupulting items record using vectors 87. Prg for bank transaction 88. Prg for counting number of days between any two different date 89. Prg for exception handling 90. Prg for throwing exception 91. Prg for exception 92. Prg to throw exception when number is invalid 93. Prg for renaming the current thread 94. Prg for demonstrating strat method of thread 95. Prg to demonstrate sleep and and join method of thread 96. Prg to demonstrate yield method of thread 97. Prg for demonstrating thread in class inheritance 98. Threading program using runnable interface 99. Thread program using sleep 100. Java Applet program for creating fancy round eye 101. Java Applet program for creating fancy face 102. Java Applet program for drawing string 103. Java Applet program for creating fancy face 104. Java Applet program for displaying fancy fonts 105. Java Applet program for drawing and filling rectangle 106. Java Applet program for creating house 107. Java Applet program to show lifecycle of applet 108. Java Applet program for creating polygon 109. Java Applet program for creating rectangle 110. Java Applet program for drawing line 111. Java Applet program for getting applet width and height 112. Interview Question & Answer part 1 113. Interview Question & Answer part 2 114. Interview Question & Answer part 3 115. Interview Question & Answer part 4 116. Interview Question & Answer part 5 117. Interview Question & Answer part 6 118. Interview Question & Answer part 7 119. Interview Question & Answer part 8 120. Interview Question & Answer part 9 121. Interview Question & Answer part 10 122. Interview Question & Answer part 11
Pr.Pg Next Pg

Constructors in Java tutorials

  • A constructor is a special method that is used to initialize a newly created object and is  called just after the memory is allocated for the object.

  • It can be used to initialize the objects ,to required ,or default values at the time of object creation.

  • It is not mandatory for the coder to write a constructor for the class. By default java provide one constructor for every class.

  • Constructor has the same name as that of class, they are called or invoked when an object of class is created and can't be called explicitly.

  • Attributes of an object may be available when creating objects if no attribute is available then default constructor is called, also some of the attributes may be known initially.

  • It is optional to write constructor in a class but due to their utility they are used.

  • If no user defined constructor is provided for a class, compiler initializes member variables to its default values as below:

    1. numeric data types are set to 0

    2. char data types are set to null character('í)

    3. reference variables are set to null

  • In order to create a constructor you must follow some rules as below:
    1. Constructors can use any access modifier, including private. A private constructor means only code within the class itself can instantiate an object of that type, so if the private constructor class wants to allow an instance of the class to be used, the class must provide a static method or variable that allows access to an instance created from within the class.

    2. Constructor has same name as class.

    3. Constructor should not return any value not even void.

Note: Java is a garbage collected language you cannot predict when an object will be destroyed. Hence there is no direct equivalent of a destructor in Java.

 

Difference between Constructor and Method

Constructor

Method

Constructor have same name as class

Method may or may not have the same name as class if methods having same name as class then it must have any return type including void.

Constructor is a special method of a class but canít be invoked directly by method call. Constructor is not a member of a class as it can neither be inherited nor invoked using dot(.) operator.

Methods are member of class and can invoked using dot(.) operator.

Constructor has no explicit return type.

Method have a return type, if there is nothing to return, the return type must be void.

Constructor is used to initialize the objects, members of object and then execute statements if any.

Method is used to execute statements or logical code block encapsulated inside a method.

A constructor can never be abstract or static.

A method is of two types defined i.e implemented or undefined i.e abstract. The method implementation can be further categorized as static or non-static. An abstract method canít be static or final.

Constructor can be invoked by either new ClassName(), this(args if any), super(args if any), getInstance().

Method can be invoked by Class name in case of static method or by object/this in case of non-static method.

 

Syntax: 

<modifier> class <class name>

{

<constructor_name_same_as_class>

{

//Body of constructor;

}

}

 

Example: Java program to demonstrate constructor

 Sam Madam

//Program name ConstructorDemo.java

// Java program to demonstrate constructor

public class ConstructorDemo

{

int Num1;

int Num2;

ConstructorDemo()

{

Num1 = 10;

Num2 = 20;

System.out.println("I am inside Constructor");

System.out.println("Value1 === "+Num1);

System.out.println("Value2 === "+Num2);

System.out.println("End of Constructor");

}

public void display()

{

System.out.println("I am inside display method");

System.out.println("Value1 === "+Num1);

System.out.println("Value2 === "+Num2);

System.out.println("End of display method");

}

public static void main(String args[])

{

ConstructorDemo d1 = new ConstructorDemo();

d1.display();

}

}

Output

I am inside Constructor

Value1 === 10

Value2 === 20

End of Constructor

I am inside display method

Value1 === 10

Value2 === 20

End of display method

 

Parameterized constructors

  • Since a constructor is also a special method it can have arguments.

  • An argument is called as a parameter.

  • Parameters appearing in the call statement are known as the formal parameters and in definition are called as actual parameters.

  • Since constructor is called at the point of definition of object, the values to constructor are sent through definition.

  • The values are specified after object name in( ).

  • There can be more than one constructor , which must be differentiated by the number of arguments or their data types.

 

 

Example: Java program to illustrate parameterized constructor

 Sam Madam

// Java program to illustrate parameterized constructor

public class Test

{

Test(int a, int b)

{

System.out.println("Value of a :"+a);

System.out.println("Value of b :"+b);

}

public static void main(String[] args)

{

Test t1=new Test(10, 20);

}

}

Output

Value of a :10

Value of b :20

Pr.Pg border                                              Next Pg