Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Constructor

Andr's picture

Java ArrayList Constructors

2.5
Your rating: None Average: 2.5 (4 votes)
ArrayList Constructors

1) ArrayList()
Creates an empty ArrayList.

Example
ArrayList arrayList = new ArrayList();

2) ArrayList(int capacity)
Creates an ArrayList with specified initial capacity.

Example
ArrayList arrayList = new ArrayList(10);

3) ArrayList(Collection c)
Andr's picture

String Constructors

0
Your rating: None
String Constructors

String class provides various types of constructors to create String objects. Some of them are,

String()
Creates a new String object whose content is empty i.e. "".
Megha's picture

What will happen when you compile and run the following code?

3.5
Your rating: None Average: 3.5 (2 votes)
  1. public class BooleanDemo {
  2.     public static void main(String args[]){
  3.         Boolean bObj = new Boolean("Hello World!");
  4.         System.out.println(bObj);
  5.     }
  6. }
a. Program will not compile
b. Program will throw runtime error
c. Program will print false
d. Program will print true

Answer:
Bharat's picture

How to call super class constructor from subclass constructor?

3.5
Your rating: None Average: 3.5 (2 votes)
Constructor of the super class can be called using super.

 
Bharat's picture

How to call a constructor from another constructor within the class?

0
Your rating: None
Different constructor of the same class can be called using “this”. e.g.
  1. public class Q1 {
  2.  Q1(){
  3.   System.out.println("Default Constructor");
  4.  }
  5.  
  6.  Q1(String msg){
  7.   this(); //calls default constructor
  8.   System.out.println("One Argument Constructor");
  9.  }
  10. }
Bharat's picture

What happens if you create one argument constructor and do not create constructor without argument?

5
Your rating: None Average: 5 (1 vote)
If class does not define any constructors, Java automatically creates default constructor with no arguments.  However, if constructor with arguments is specified, Java does not create default one.
 
Nothing happens if we do not specify the default constructor. However,
Bharat's picture

What is difference between method and constructor?

4
Your rating: None Average: 4 (6 votes)
Constructor is a special member function of a class which is used to create object of that class. Constructor has a same name as that of the class. It also does not have any return type. However, constructor may have arguments.
 
Andr's picture

Java Programming Tutorial - Multiple Constructors

Video Tutorial: 
See video
Andr's picture

Java Programming Tutorial - Constructors

Video Tutorial: 
See video
Syndicate content