About Question enthuware.ocajp.i.v7.2.1123 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Michailangelo

About Question enthuware.ocajp.i.v7.2.1123 :

Post by Michailangelo »

Doesn't the import statement allow the usage of A class constructor?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1123 :

Post 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.
If you like our products and services, please help us by posting your review here.

ETS user

Re: About Question enthuware.ocajp.i.v7.2.1123 :

Post 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?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1123 :

Post 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.
If you like our products and services, please help us by posting your review here.

pbonito
Posts: 13
Joined: Sat May 16, 2015 12:38 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1123 :

Post 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

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1123 :

Post 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.
If you like our products and services, please help us by posting your review here.

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1123 :

Post 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.

mihhay
Posts: 10
Joined: Wed May 09, 2018 12:48 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1123 :

Post by mihhay »

I have the same issue...

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1123 :

Post by admin »

What is the issue?
If you like our products and services, please help us by posting your review here.

mihhay
Posts: 10
Joined: Wed May 09, 2018 12:48 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1123 :

Post 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 ?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1123 :

Post 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??
If you like our products and services, please help us by posting your review here.

olsongrant
Posts: 8
Joined: Mon Nov 02, 2020 10:21 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1123 :

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1123 :

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 40 guests