Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Sun Sep 30, 2012 12:21 pm
by Michailangelo
Doesn't the import statement allow the usage of A class constructor?
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Sun Sep 30, 2012 5:49 pm
by admin
You might want to go through the detailed explanation that is provided again. Import just gives you a short cut. (i.e. instead of writing com.abc.mypackage.MyClass c; you can just write: MyClass c;). It doesn't change access rights.
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Thu Jan 03, 2013 5:45 pm
by ETS user
From one of the other tests I noted the comment "the access type of a constructor is the same as the access type of the class". Is this true if no default constructor is provided?
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Thu Jan 03, 2013 6:21 pm
by admin
ETS user wrote:From one of the other tests I noted the comment "the access type of a constructor is the same as the access type of the class". Is this true if no default constructor is provided?
This is true only for the default constructor that is provided by the compiler (i.e. if you don't provide any constructor at all) and not for the constructor that you explicitly provide.
For an explicitly provided constructor you can have any access type. For example, a public class can have a private constructor.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Tue May 19, 2015 1:30 pm
by pbonito
What about new B()?
It is a strange statement, why the compiler permits it?
There is no way to access methods and fields of the object. Is it immediately eligible for garabage collector?
Thanks
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Tue May 19, 2015 8:05 pm
by admin
The statement is creating an object of class B. There is nothing wrong with it. Even if there is no reference to the object, the constructor is executed and that constructor might have code with business logic. So there is no reason for a compiler to disallow it.
Yes, it will be eligible for gc because there is no active reference to the object.
HTH,
Paul.
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Fri May 29, 2015 1:18 am
by subhamsdalmia
Changed to
Code: Select all
package a;
public class A{
protected A(){ }
public void print(){ System.out.println("A"); }
}
Still doesnt print anything.
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Wed May 09, 2018 12:50 pm
by mihhay
I have the same issue...
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Wed May 09, 2018 2:11 pm
by admin
What is the issue?
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Fri May 11, 2018 12:13 am
by mihhay
In "public class A" I added :protected A(){ }" - as the code above ( instead of just A(){} ) and I expect to see in the console : "A" but this is not happening. Is not printing anything...and I am wondering why ?
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Fri May 11, 2018 3:32 am
by admin
Not sure what making the constructor protected has to do with printing an output. Why are you expecting that it will print something if you make the constructor protected? Do you see print method getting called anywhere??
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Wed Nov 18, 2020 5:50 pm
by olsongrant
I think the test-taker typically expects there to be an answer choice representing how the program would behave, if only it were to compile. From that perspective, the "It will print A" and "It will print B" answer choices might be unsatisfying, as the print() method is never called by the main() method or by the constructors. Maybe some folks would consider it an improvement if "The program runs but nothing is printed" were added as an answer choice.
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Posted: Wed Nov 18, 2020 11:46 pm
by admin
Thanks for your suggest. There is "None of the above" option already there though.
Also, when you have a definite answer "It will not compile", then there is little scope for any output at runtime.