8 Replies - 10039 Views - Last Post: 26 April 2011 - 08:05 AM Rate Topic: -----

#1 qxc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-April 11

Declaring multiple variables?

Posted 26 April 2011 - 06:15 AM

Hey ppl, I am very new to Java and I really dislike having to declare my variables line by line like so:
int x = 2;
int y = 3;
int z = 20;
int sum = (z + x) - y;


Is there a way for me to declare variables differently?
Is This A Good Question/Topic? 0
  • +

Replies To: Declaring multiple variables?

#2 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Declaring multiple variables?

Posted 26 April 2011 - 06:17 AM

you can declare variables on one line like so:
int x = 2, y = 3, z = 20; //declared 3 variables on one line
int sum = (z+x)-y;


This is possible if the variables are of the same type, any calculations however I personally declare on its own line so as to make my code more readable.

This post has been edited by v0rtex: 26 April 2011 - 06:17 AM

Was This Post Helpful? 2
  • +
  • -

#3 qxc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-April 11

Re: Declaring multiple variables?

Posted 26 April 2011 - 06:19 AM

Thanks a lot, also what exactly is a global variable?
Was This Post Helpful? 0
  • +
  • -

#4 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Declaring multiple variables?

Posted 26 April 2011 - 06:23 AM

A instance variable is a variable that has variable scope to the whole program (can be accessed by any method from the program), this would be an example of a instance variable:
public class car  {
private String age = 20; // instance variable :P/>
car () { }
public String getAge () {
return age;
}


Even though the variable was not declared in the getAge method, it still has variable scope as it is global, it is concerned as global as it was declared at the top of our class.
In general, I would rather stay away from global variables as any method can change the data inside them and this makes them unreliable and often prone to errors if you are working with the variable repeatedly.
**EDIT: This is a instance variable, I read the OP's question wrong**
A global variable is:
a variable that is declared as public static, this makes it accesible to all the methods in your program and by any classes that inherit the class with that method in it. It is explained very well by ShaneK below.

This post has been edited by v0rtex: 26 April 2011 - 06:57 AM

Was This Post Helpful? 1
  • +
  • -

#5 qxc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-April 11

Re: Declaring multiple variables?

Posted 26 April 2011 - 06:24 AM

Thank you v0rtex.
Was This Post Helpful? 0
  • +
  • -

#6 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Declaring multiple variables?

Posted 26 April 2011 - 06:30 AM

View Postv0rtex, on 26 April 2011 - 08:23 AM, said:

public class car  {
private String age = 20; // global variable
car () { }
public String getAge () {
return age;
}



That's not a global variable in Java, it's an instance variable, and those are okay. They're actually kind of the preferred way of creating variables you'll need for more than a single method. Also, you can't put an integer in a String variable...

A global variable in Java is a public static variable, which makes it able to be accessed by any method in the program. For example:
public class ThisClassIsAwesome {
    public static int awesomeLevel = 20;
}


Now you can access awesomeLevel from every (static) method in ThisClassIsAwesome and even methods in other classes, just by saying
ThisClassIsAwesome.awesomeLevel
But that's not recommended as it destroys encapsulation, which is one of the finer points of OOP (Object-oriented programming)

Yours,
Shane~

This post has been edited by ShaneK: 26 April 2011 - 06:36 AM

Was This Post Helpful? 1
  • +
  • -

#7 codejunkie12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 20-April 11

Re: Declaring multiple variables?

Posted 26 April 2011 - 06:48 AM

I was also looking for how to do this! thanks all
Was This Post Helpful? 0
  • +
  • -

#8 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: Declaring multiple variables?

Posted 26 April 2011 - 06:55 AM

lol sorry, I read it wrong :P Thought it said instance variable :/
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2065
  • View blog
  • Posts: 4,285
  • Joined: 11-December 07

Re: Declaring multiple variables?

Posted 26 April 2011 - 08:05 AM

You can also declare variables like this if you don't want to set initial values right away.

int x, y, z;
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1