• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static Methods

 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had an original code:

I was then supposed to change Triangle to a static method and delete the class TriangleCalc. What is a static method and how do they work? I tried what I could do (below), and got an error. I am unsure of how to include a static method in a main method. Here's my code:


Thank you!!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A static method is a method defined with the "static" keyword.

You say you "got an error", but neglected to provide any information about the error.
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry about that, I tend to ask for help without providing critical information :P One error says illegal start of expression. Another says need a semi colon. Why would I ever need a semi colon between two brackets?



Thanks!!!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're trying to create a method smack-dab in the middle of another method--that's not legal Java.
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The assignment said not to delete the class from the first assignment- should I just create another class?

Thanks,
Theresa
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See how main is declared? Main is a static method. How might you declare your own static method?
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
by creating another static main method, and putting the class there?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. Maybe check out some Java tutorials--all you're doing is creating a new method and removing the Triangle class. You might be overthinking this.
 
Theresa Marlin
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I will, thanks! Do you have any tutorials to recommend for static?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really.

As I said: main is already a static method. In other words, why would creating a new static method look any different than the main method???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic