Discussion :: Declarations and Access Control - Pointing out the correct statements (Q.No.4)
Stinger said: (Mar 18, 2011) | |
In the above explanation, the class has default access but the constructor is public? |
Tiago said: (Jan 19, 2012) | |
In the example above, class CoffeeCup is public, so the default constructor is public. If CoffeeCup had been given package access, the default constructor would be given package access as well. This is wrong. CoffeeCup on the example is default access. |
Iroshan said: (Jun 10, 2014) | |
Default constructor can be private as well. |
Ankit said: (Jun 24, 2014) | |
Please explain me this : 5. The compiler creates a default constructor only when there are no other constructors for the class. class A{ int a; int b; int c; A(int b,int c){ this.b=b; this.c=c; }} class B{ public static void main(String args [ ]){ A obj = new A(10,20); System.out.println("a="+obj.a+" b="+obj.b+" c="+obj.c); } } Output:a=0 b=10 c=20 How does variable a is assigned a value 0 if there is no default constructor made by the compiler ? |
Pavani said: (Mar 5, 2015) | |
Because 0 is default value for integer. Whenever object is created then memory allocated to instance variables (if there is no initialization for those variables then it takes default values) at the same time constructor also called. so B and C gets directly 10 and 20 and there is no value for A, so it gets default value. |
Prajwal said: (Nov 14, 2018) | |
5) is correct. The compiler creates a default constructor if you do not declare any constructors in your class. 5) The compiler creates a default constructor only when there are no other constructors for the class. Only is the difference. The default constructor is created when there is no param constructor defined. |
Post your comments here:
Name *:
Email : (optional)
» Your comments will be displayed only after manual approval.