Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call method from another class

 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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":
 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting an Illegal character \160 error
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change This


to this


The first one- Its always true.

And please post the complete error/stack trace.
 
Dustin Schreader
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh oops thanks for that. But The error still persists.
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch that was it thanks!
 
Ayoma Wijethunga
Ranch Hand
Posts: 38
Firefox Browser IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohamed sanaullah wrote:Change This


to this
. . .
Surely you mean if (isFullStack) . . . You should never use == true or == false.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Surely you mean if (isFullStack) . . . You should never use == true or == false.


Oh yeah, better than using ==.
 
Kirsty Beaton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kirsty Beaton
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't realise it would compile like that (with one "=").

I'll take your word for it.
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting, I had no idea. I will read up on that. Thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic