The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assert and private constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assert and private constructor" Watch "Assert and private constructor" New topic

Assert and private constructor

Linus Nikander

Joined: May 21, 2004
Posts: 9
In a recent mock-exam that i attempted the following assertion was made:

String y = "test ";
String x;

assert(x > 1) : x = y + 1;

is this valid because x resolves to test 1 (i.e a value) ?

and will the following assertion be valid

String test = null;

assert (x > 1) : test;

Another question tested the functionality of a private constructor:

Given the following:

class Parent{

private Parent(){


class Child extends Parent{


Now if I, in some other context, try to instantiate a Child:

Child myChild = new Child();

I should get an error since the call to super(); won't be allowed, right ?

A followup to this. Is the only way to circumvent the above problem to either make an overloaded (non-private) constructor in Parent and then call that explicitly from the Child() constructor, or to simply remove the private, or are there other ways around this ?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I suggest you go write some code

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
It is sorta covered in the JavaRanch Style Guide.
subject: Assert and private constructor