constructor from diffrent class

T

Thomas

As above I have two classes : abstract Stack and class Function, with
Function holding one instance of Stack. Now, when calling constructor for
Function I would also to call it for Stack. The problem is I got :
unresolved symbol error: Stack(). What should I do ? Both files Stack.java
and Function.java are in project.

pseudocode :
abstract Function{
Stack S;

Function(){
S = Stack();
}

}
 
R

rossum

As above I have two classes : abstract Stack and class Function, with
Function holding one instance of Stack. Now, when calling constructor for
Function I would also to call it for Stack. The problem is I got :
unresolved symbol error: Stack(). What should I do ? Both files Stack.java
and Function.java are in project.

pseudocode :
abstract Function{
Stack S;

Function(){
S = Stack();
}

}
When you call any constructor, you need the "new" construction:

S = new Stack();

This will not compile in your particular case because you have
declared your Stack class to be abstract. Abstract classes cannot be
instantiated, and the compiler will tell you so.

One way to initialise the stack member variable in Function is to
initialise it from a constructor parameter:

public Function(Stack ss) {
S = ss;
}

This will compile.

Inside your program you could derive a non-abstract class from your
abstract Stack and you can pass an instance of that derived class into
your Function constructor:

class ConcreteStack extends Stack { ... }

ConcreteStack myCStack = new ConcreteStack();

Function myFunction = new Function(myCStack);

rossum
 
T

Thomas

Uzytkownik "rossum said:
When you call any constructor, you need the "new" construction:

S = new Stack();

This will not compile in your particular case because you have
declared your Stack class to be abstract. Abstract classes cannot be
instantiated, and the compiler will tell you so.

One way to initialise the stack member variable in Function is to
initialise it from a constructor parameter:

public Function(Stack ss) {
S = ss;
}

This will compile.

Inside your program you could derive a non-abstract class from your
abstract Stack and you can pass an instance of that derived class into
your Function constructor:

class ConcreteStack extends Stack { ... }

ConcreteStack myCStack = new ConcreteStack();

Function myFunction = new Function(myCStack);

rossum
Thx, I'm just to tired.
 
R

Roedy Green

pseudocode :
abstract Function{
Stack S;

Function(){
S = Stack();
}

}

Please post real code. This is too far from real code. Please have a
look at http://mindprod.com/jgloss/codingconventions.html

What I think your question might be is this:

I have a circular reference:

class F {
Stack s;
....
}

class Stack{
F f ;
....
}

If I compile F first javac can't find Stack. If I compile Stack
first, it can't find F. I'm hosed. What do I do?

The answer is at http://mindprod.com/jgloss/javacexe.html#CIRCULAR
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,789
Messages
2,569,634
Members
45,342
Latest member
Sicuro

Latest Threads

Top