• Post Reply Bookmark Topic Watch Topic
  • New Topic

multiple variables in Static Methods  RSS feed

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys
Is it possible to use multiple variables in a static method, or do I have to make multiple static methods to handle addition of each variable?

I'm getting
 
Michael Giara
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see, I have to declare the data types of the variables individually.
Michael Giara wrote:
 
Greenhorn
Posts: 5
Netbeans IDE Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your declarations are incorrect. A correct example:

That is, you must write variable's type before the variable's name.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael. Welcome to the Ranch!

Yes, you can do that. But you need to give a type for each variable. So:

That's what the error message meant. It interpreted the parameter as the type name, and was then wondering why there was no following identifier.

Edit: as you've worked out yourself!
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And note that static has nothing to do with it. The same rules apply for all method and constructor argument lists.

When you're declaring variables that aren't in an argument list, you can name multiple variables with a single type declaration:


but you shouldn't. It's non-standard and makes your code harder to read.

Also 'l" (lowercase "EL" is a horrible variable name, as are I (uppercase "EYE") and O (uppercase "OH"). They look like the numbers 1 and 0.
 
Michael Giara
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for your help everyone!!


Thanks for the pointer Jeff.
 
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declarations as you had in your first post are permissible in some languages, but not in Java.
 
Marshal
Posts: 4052
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While we're on the topic of Java code syntax, the Java Code Conventions say that the "opening brace should be at the end of the line that begins the compound statement". The old "C" style of brace placement isn't generally used for Java code although I still do come across it from time to time in legacy code.

So your code should be formatted as follows:
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:So your code should be formatted as follows:

You need to be careful with advice like that. Even though I tend to use the same brace placement as you, there's absolutely nothing wrong with the code Michael supplied. The fact is that naming conventions are much more important.

@Michael: Whichever style you choose, the only thing I'd say is: indent properly and be consistent - and I see no evidence that you haven't been.

Winston
 
Campbell Ritchie
Marshal
Posts: 56605
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you trying to calculate areas outside their own classes?
Each class should be responsible for itself, so each class should calculate its own area.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!