Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

constructor from diffrent class

Thread view: 
Thomas - 29 Jul 2007 19:01 GMT
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();
}

}
rossum - 29 Jul 2007 19:57 GMT
>As above I have two classes : abstract Stack and class Function, with
>Function holding one  instance of Stack. Now, when calling constructor for
[quoted text clipped - 11 lines]
>
>}

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
Thomas - 29 Jul 2007 20:10 GMT
> >As above I have two classes : abstract Stack and class Function, with
> >Function holding one  instance of Stack. Now, when calling constructor for
[quoted text clipped - 40 lines]
>
> rossum

Thx,  I'm just to tired.
Roedy Green - 29 Jul 2007 21:36 GMT
>pseudocode :
>abstract Function{
[quoted text clipped - 5 lines]
>
>}

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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.