About Question enthuware.ocajp.i.v7.2.1123 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1123 :
Doesn't the import statement allow the usage of A class constructor?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1123 :
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.
Re: About Question enthuware.ocajp.i.v7.2.1123 :
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?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1123 :
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.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?
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.
-
- Posts: 13
- Joined: Sat May 16, 2015 12:38 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1123 :
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
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
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1123 :
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.
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.
-
- Posts: 32
- Joined: Sat May 02, 2015 11:57 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1123 :
Changed to
Still doesnt print anything.
Code: Select all
package a;
public class A{
protected A(){ }
public void print(){ System.out.println("A"); }
}
-
- Posts: 10
- Joined: Wed May 09, 2018 12:48 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1123 :
I have the same issue...
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1123 :
What is the issue?
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Wed May 09, 2018 12:48 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1123 :
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 ?
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1123 :
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.
-
- Posts: 8
- Joined: Mon Nov 02, 2020 10:21 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1123 :
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.
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1123 :
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.
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.
Who is online
Users browsing this forum: No registered users and 158 guests