This question already has answers here:
What is a StackOverflowError?
(16 answers)
Closed 4 years ago.
Below is a program given for assignment. Request you to help on the below output getting as "Expected output". It providing error as "Exception in thread "main" java.lang.StackOverflowError".
class A
{
{
new B();
}
static class B
{
{
new A().new C();
}
}
class C
{
{
System.out.println("Expected output");
}
}
}
public class MainClass
{
public static void main(String[] args)
{
new A();
}
}
You call new A(), which calls new B(), which calls new A() again, which calls new B() again, and it goes on and on until you can't create new objects anymore (thus StackOverflowError).
You should stop creating A() or B() at some point
class A
{
{
new B();
}
static class B
{
static {
new A().new C();
}
}
class C
{
{
System.out.println("Expected output");
}
}
}
public class MainClass
{
public static void main(String[] args)
{
new A();
}
}
The Anonymous block is executed before any of the constructor
static block is executed before loading a static class
Related
In Javadoc, I can see Class ClassCastException's Constructor with String parameter. But ClassCastException's Instance automatically is created (by JVM), I don't know how to use ClassCastException's Constructor.
In my code, I want to get a result "wrong", not "B cannot be cast to C".
class Prac {
public static void main(String[] args) {
try {
ClassCastException e = new ClassCastException("wrong");
A a = new B();
C c = (C)a;
}
catch(ClassCastException e) {
System.out.println(e.getMessage());
}
}
}
class A {
}
class B extends A {
}
class C extends A{
}
Result : B cannot be cast to C
If you want the result "wrong", then just print "wrong".
public static void main(String[] args) {
try {
A a = new B();
C c = (C)a;
}
catch(ClassCastException e) {
System.out.println("wrong");
}
}
No need to go tinkering around with the exception.
Several times I thought it will be good to have overridable constructor in Java.
"Overridable" means the logic of construction can be overriden and/or extended in descending classes in the same way it is possible to override normal methods, i.e. with ability to call parent method AFTER the child.
This task can be formulated as to have a method, say, called init() which is called at construction time, but only in the last constructor of the stack.
Like:
public class InitializationOverride {
public static class A {
A() {
System.out.println("Constructor of A");
}
void init() {
System.out.println("Init of A");
}
}
public static class B extends A {
B() {
System.out.println("Constructor of B");
}
#Override
void init() {
System.out.println("Init of B");
}
}
public static class C extends B {
C() {
System.out.println("Constructor of C");
}
#Override
void init() {
System.out.println("Init of C");
}
}
public static void main(String[] args) {
new A(); // should print "Constructor of A, Init of A"
new B(); // should print "Constructor of A, Constructor of B, Init of B"
new C(); // should print "Constructor of A, Constructor of B, Constructor of C, Init of C"
}
}
The obvious way is to write
public static void main(String[] args) {
new A().init();
new B().init();
new C().init();
}
but this doesn't guarantee init() is not forgotten to call.
Is it possible to do somehow?
UPDATE
It is not known at design time, which class will be "last". It is expected, that class tree will be developed in future.
UPDATE 2
Here is the solution with reflection and constructor code requirement to call currentStage() at the end:
public class InitializationOverride {
public static class A {
A() {
System.out.println("Constructor of A");
currentStage(A.class);
}
void currentStage(Class<?> cls) {
if( cls == getClass() ) {
init();
}
}
void init() {
System.out.println("Init of A");
}
}
public static class B extends A {
B() {
System.out.println("Constructor of B");
currentStage(B.class);
}
#Override
void init() {
System.out.println("Init of B");
}
}
public static class C extends B {
C() {
System.out.println("Constructor of C");
currentStage(C.class);
}
#Override
void init() {
System.out.println("Init of C");
}
}
public static void main(String[] args) {
new A(); // should print "Constructor of A, Init of A"
new B(); // should print "Constructor of A, Constructor of B, Init of B"
new C(); // should print "Constructor of A, Constructor of B, Constructor of C, Init of C"
}
Is it possible to write simpler?
Constructors shouldn't call overridable methods. If invoking of such method is necessary the better solution is makes constructors protected and provide static factory methods:
public class InitializationOverride {
public static class A {
protected A() {
System.out.println("Constructor of A");
}
public static A newInstance(){
A a = new A();
a.init();
return a;
}
protected void init() {
System.out.println("Init of A");
}
}
public static class B extends A {
protected B() {
System.out.println("Constructor of B");
}
public static B newInstance(){
B b = new B();
b.init();
return b;
}
#Override
protected void init() {
System.out.println("Init of B");
}
}
public static class C extends B {
protected C() {
System.out.println("Constructor of C");
}
public static C newInstance(){
C c = new C();
c.init();
return c;
}
#Override
protected void init() {
System.out.println("Init of C");
}
}
public static void main(String[] args) {
A.newInstance(); // should print "Constructor of A, Init of A"
B.newInstance(); // should print "Constructor of A, Constructor of B, Init of B"
C.newInstance(); // should print "Constructor of A, Constructor of B, Constructor of C, Init of C"
}
}
Edit
More explanation: Such solution provide benefits but also drawbacks. You should to provide a contract for classes (i.e. in Javadoc) that sub-classes that extends your class should follow this standard of objects creation. Also it creates more code. The profit is that objects created in that way:
C obj = C.newInstance()
...are always fully initialized and there is no need to remember for call init() method explicite.
Remember that it also will be the only method for create object outside the class' package (constructor won't be available), but inside same package constructor will be still available (protected methods are available inside same package)
In Java, when child class is instantiated, default constructor of parent class is always invoked (unless any other constructor is specified). Now, if you need to have a common code that needs to be executed for all the classes, it's recommended to put it in constructor. However, if you want something to be executed only in the last class in the hierarchy then (a) you can write it into the last constructor itself or (b) write an initialisation block, below example demonstrates this:
public class Test extends Test2{
public Test(){
System.out.println("In test");
System.out.println("Init last");
}
{
System.out.println("Init");
}
public static void main(String[] args) {
new Test();
}
}
class Test2{
public Test2(){
System.out.println("In test 2");
}
}
Just change your a class constructor like this, each object init method will call by calling this.init(), you required to change just most upper class constructor.Because at time of object creation parent class constructor will definitely call
public class Test {
public static class A {
public A() {
this.init();
}
void init() {
System.out.println("Called in A");
}
}
public static class B extends A {
#Override
void init() {
System.out.println("Called in B");
}
}
public static class C extends B {
#Override
void init() {
System.out.println("Called in C");
}
}
public static void main(String[] args) {
new A(); // should "Called in A" printed
new B(); // should "Called in B" printed
new C(); // should "Called in C" printed
}
}
Use super.init() for this to call root parent class init() .
Class A
{
B b1=new B();
}
Class B
{
A a1=new A();
}
I'm talking about something like this? Is it possible?
Yes, you can. The following compiles just fine:
class A {
B b1 = new B();
public A() {
System.out.println("A constructor");
}
}
class B {
A a1 = new A();
public B() {
System.out.println("B constructor");
}
}
public class HelloWorld {
public static void main(String []args) {
A a0 = new A();
System.out.println("Done");
}
}
However, as shown in the output, it's generally a bad idea:
Exception in thread "main" java.lang.StackOverflowError
at B.<init>(HelloWorld.java:8)
at A.<init>(HelloWorld.java:3)
at B.<init>(HelloWorld.java:8)
at A.<init>(HelloWorld.java:3)
at B.<init>(HelloWorld.java:8)
:
at A.<init>(HelloWorld.java:3)
at B.<init>(HelloWorld.java:8)
at A.<init>(HelloWorld.java:3)
at B.<init>(HelloWorld.java:8)
The fact that construction of an A tries to create a B, and construction of a B tries to create an A, means that you'll get caught in infinite regress, eventually running out of stack space.
You can safely have two objects refer to each other but it's generally done after the construction phase, something like:
class A {
B b;
public A() {
System.out.println("A constructor");
}
public void setOther(B bx) {
System.out.println("A linker");
b = bx;
}
}
class B {
A a;
public B() {
System.out.println("B constructor");
}
public void setOther(A ax) {
System.out.println("B linker");
a = ax;
}
}
public class HelloWorld{
public static void main(String []args){
A a0 = new A();
B b0 = new B();
a0.setOther(b0);
b0.setOther(a0);
System.out.println("Done");
}
}
The output of that shows:
A constructor
B constructor
A linker
B linker
Done
interface A {
void show();
}
public class Static {
public static void main(String args[]) {
A a = new A(){
public void show(){
System.out.println("In anonymous Class");
A b =new A(){
public void show(){
System.out.println("In nested Anonymous Class");
}
};
}
};
//a.show();
}
}
If I want the to print "In nested Anonymous Class", what should I use instead of a.show()?
//EDITED LATER
Thanks guys But unfortunately mis-typed the code....I didn't mean anonymous class inside a method...but inside the class itself. Sorry for the mistake. Here is the corrected code
interface A {
void show();
}
public class Static {
public static void main(String args[]) {
A a = new A() {
public void show() {
System.out.println("In anonymous Class");
};
A b = new A() {
public void show() {
System.out.println("In nested Anonymous Class");
}
};
};
a.show();
}
}
Normally, it's not possible, since A is an interface and interfaces don't have fields. However, it is possible to access this field using reflection. It is a bit of hack though and I wouldn't suggest using this in the "real world"!
interface A {
void show();
}
public class Static {
public static void main(String args[]) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException {
A a = new A() {
public void show() {
System.out.println("In anonymous Class");
};
public A b = new A() {
public void show() {
System.out.println("In nested Anonymous Class");
}
};
};
// Get the anonymous Class object
Class<? extends A> anonymousClass = a.getClass();
// Get field "b"
Field fieldB = anonymousClass.getField("b");
// Get the value of b in instance a and cast it to A
A b = (A) fieldB.get(a);
// Show!
b.show();
}
}
Note: a better way might be to simply declare a getter on your interface for variable b.
make a call to b.show(); just after class declaration.
A b =new A(){
public void show(){
System.out.println("In nested Anonymous Class");
}
};
b.show();
There is nothing you should use instead of a.show(). That line should be where you put it, and uncommented. Additionally you need b.show() inside:
public static void main(String args[]) {
A a = new A(){
public void show(){
System.out.println("In anonymous Class");
A b =new A(){
public void show(){
System.out.println("In nested Anonymous Class");
}
};
b.show();
}
};
a.show();
}
I need to know the output of this code. But it's not working. Maybe the code is wrong.
I'm still learning how to use Java, and I tried fixing this for hours but still no luck.
Here is the code:
public class A
{
public A()
{
System.out.println ("A");
}
}
public class B extends A
{
public B()
{
System.out.println ("B");
}
}
public class C extends B
{
public C()
{
System.out.println ("C");
}
}
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
}
Can anyone tell me what is wrong or missing in the code?
Put your main method in a class.
Filename : DemoClass.java
class A
{
public A()
{
System.out.println ("A");
}
}
class B extends A
{
public B()
{
System.out.println ("B");
}
}
class C extends B
{
public C()
{
System.out.println ("C");
}
}
public class DemoClass {
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
}
}
Another point here is, you can have only public class in a file, so your A B and C all class can't be public in same java file.
Your java file name must be same as public class name. i.e. here DemoClass is public class so file name will be DemoClass.java
Java doc for getting started : getting started with java
For example:
public class Example {
public static void main(String...args) {
new C();
}
public static class A {
public A() {
System.out.println("A");
}
}
public static class B extends A {
public B() {
System.out.println("B");
}
}
public static class C extends B {
public C() {
System.out.println("C");
}
}
}
Also note that this might not print what you would expect. It would actually print:
A
B
C
Why? Constructors are always chained to the super class.
You can, but it is not recommended, nest your classes in a file. It is perfectly valid.
Notice in the output below that each successive child calls its parent's default constructor (super()) implicitly.
I recommend you create the files: A.java, B.java, C.java, and InheritenceTest.java.
public class InheritenceTest {
public class A {
public A() {
System.out.println("A");
}
}
public class B extends A {
public B() {
System.out.println("B");
}
}
public class C extends B {
public C() {
System.out.println("C");
}
}
public static void main(String args[]) {
InheritenceTest i = new InheritenceTest();
A a = i.new A();
B b = i.new B();
C c = i.new C();
}
}
Output:
A
A
B
A
B
C
Warning:
You shouldn't have more than 1 public classes in 1 java file, not recommended. However, it could still work if you didn't use the 'public' identifier (or by using static or inside another class). But for a starter, I would recommend you to have them all in separate files.
Error:
Your main method does not belong to any class. I propose you create another class that includes the public static void main method to test your application.
Info: keep a look at inheritance as your printings might not be what you expect. (Constructor of class B calls the constructor of A, and constructor of class C calls the constructor B which in turn calls the constructor of A).
That's why you get
A
A
B
A
B
C
*due to A() it prints A, then due to B() it prints A B and finally due to C() it prints A B C.
In your case, I would try the following:
//Filename: A.java
public class A {
public A() {
System.out.println ("A");
}
}
//Filename: B.java
public class B extends A {
public B() {
System.out.println ("B");
}
}
//Filename: C.java
public class C extends B {
public C() {
System.out.println ("C");
}
}
//Filename: Test.java
//use a Test class for testing
public class Test {
public static void main(String args[]) {
A a = new A();
B b = new B();
C c = new C();
}
}