File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Call method from another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Call method from another class" Watch "Call method from another class" New topic
Author

Call method from another class

Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
I'm trying to call a method from another class and I'm just wondering the proper syntax because I'm not understanding how this works.
I have DropOutStack class that extends LinkedStackClass and I would like to call method push(T newElement) that is located in LinkedStackClass From my GuiDropOutStack class. Since super must be first I'm not really sure how to put the method where I would like to use it.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

Dustin Schreader wrote:Since super must be first ...

That rule is only for constructors. To call a method in a superclass, do something like this:

Also, this looks strange:

If this is meant to call a remove method, just call the method with its name instead of "this":


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
I'm getting a cannot find symbol error should I have added something like this


Somewhere in the GuiDropOutStack class in order for it to be able to find it from dropoutstack class that extends linkedstackclass?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

If you want to call push(T newElement) method located in the LinkedStackClass and DropOutStack extends LinkedStackClass- You can use an instance of DropOutStack to call this method. Something like


So you would be calling this method from the GuiDropOutStack class.

Also the code you have provided in you first post- Its not correct.


Mohamed Sanaulla | My Blog
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
I'm getting an Illegal character \160 error
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Change This


to this


The first one- Its always true.

And please post the complete error/stack trace.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
oh oops thanks for that. But The error still persists.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Delete the character at the end of the line:


Looks like its a-  - non-breaking space- Something that comes from copying the html.
Dustin Schreader
Ranch Hand

Joined: May 25, 2009
Posts: 74
Good catch that was it thanks!
Ayoma Wijethunga
Ranch Hand

Joined: Mar 07, 2010
Posts: 38

Dustin Schreader wrote:oh oops thanks for that. But The error still persists.


"\160" is the Unicode representation of "non-breaking space"

My suggestion is to simply select that entire line, remove it and type it without copying and pasting. You might have pasted a \160 from some html page or a PDF to your source code.

Hope this helps.

P.S.. - Great.. Seems like it was solved while i was typing..


AyomaOnline - CodeGreed
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
mohamed sanaullah wrote:Change This


to this
. . .
Surely you mean if (isFullStack) . . . You should never use == true or == false.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
Ayoma Wijethunga wrote: . . . "\160" is the Unicode representation of "non-breaking space" . . .
That is in decimal. The escape sequences don't take decimal. You want \u00a0.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Campbell Ritchie wrote:Surely you mean if (isFullStack) . . . You should never use == true or == false.


Oh yeah, better than using ==.
Kirsty Beaton
Greenhorn

Joined: Dec 06, 2010
Posts: 9
Campbell Ritchie wrote:
mohamed sanaullah wrote:Change This


to this
. . .
Surely you mean if (isFullStack) . . . You should never use == true or == false.


Mind if I ask the reasoning behind this? Is it simply a style issue?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Suppose you make a typo and forget one = (for instance, because you've been coding Visual Basic... unfortunately, I have to sometimes). You then get "f (isFullStack = true)". As a result, true is assigned to isFullStack which is then evaluated - to true, so the if always executes. Not what you had in mind, was it?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kirsty Beaton
Greenhorn

Joined: Dec 06, 2010
Posts: 9
I didn't realise it would compile like that (with one "=").

I'll take your word for it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

It only works with boolean (and Boolean, since Java 5.0). That's because an assignment has a return type as well - the variable type. So if x is an int, then x++, x = 13, x += 5 etc all return an int. Likewise, for a boolean b, b = x returns the new value for b after the assignment. This return value is perfectly legal in if, for, while etc statements. Some compilers / IDEs will warn you, but it is legal.
Kirsty Beaton
Greenhorn

Joined: Dec 06, 2010
Posts: 9
Interesting, I had no idea. I will read up on that. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Call method from another class