5 Replies - 32563 Views - Last Post: 22 June 2008 - 12:15 AM Rate Topic: -----

#1 nombre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-June 08

Sharing variables between classes

Posted 20 June 2008 - 10:52 PM

What is the proper way to get variables to be seen by both classes? Where do you declare them? Do you declare them outside of the classes or do you refer to them in a special way?
Is This A Good Question/Topic? 0
  • +

Replies To: Sharing variables between classes

#2 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 285
  • View blog
  • Posts: 1,503
  • Joined: 07-April 08

Re: Sharing variables between classes

Posted 21 June 2008 - 04:38 AM

View Postnombre, on 20 Jun, 2008 - 10:52 PM, said:

What is the proper way to get variables to be seen by both classes? Where do you declare them? Do you declare them outside of the classes or do you refer to them in a special way?


you cant declare a variable outside a class. The best method is to initialize a new instance of a class within the other class to call the first classes variables.

EX:

classone has 3 variables:

public class classone
{
     int variableone = 25;
     String variabletwo = "Your age";
     int variablethree = 18;
}



now we want to call these three variables from classtwo so we would do this:

public class classtwo
{
     classone classone = new classone();
     int newvariable = classone.variableone;
     //rest of code
}



Was This Post Helpful? 1
  • +
  • -

#3 nombre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-June 08

Re: Sharing variables between classes

Posted 21 June 2008 - 02:18 PM

rgfirefly24 does your example give the variable newvariable the value of classone.variableone at the end of the class or the beginning?
Was This Post Helpful? 0
  • +
  • -

#4 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 285
  • View blog
  • Posts: 1,503
  • Joined: 07-April 08

Re: Sharing variables between classes

Posted 21 June 2008 - 04:19 PM

the way its shown it does it at the beginning, but you can do it anywhere throughout the code of classtwo.
Was This Post Helpful? 0
  • +
  • -

#5 neotrumatrix  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 49
  • Joined: 06-October 05

Re: Sharing variables between classes

Posted 21 June 2008 - 07:25 PM

View Postnombre, on 21 Jun, 2008 - 02:18 PM, said:

rgfirefly24 does your example give the variable newvariable the value of classone.variableone at the end of the class or the beginning?


Firstly it wouldn't assign the variable at all not without the Main method ;) ...

Keep it in a constructor and create a second object or just write the whole code in the Main method sparing you the pain of creating another object ... And the order of allotment is the order of steps ...

As soon as you create an object of First class the first class variable is initialised i.e something physical is created(in memory) and then the next step assigns the new variable in second class to the value of first variable
Was This Post Helpful? 1
  • +
  • -

#6 nombre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 07-June 08

Re: Sharing variables between classes

Posted 22 June 2008 - 12:15 AM

Never mind. Sorry about taking up boxes.

This post has been edited by nombre: 22 June 2008 - 12:59 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1