Welcome to Dream.In.Code
Become a Java Expert!

Join 150,122 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,007 people online right now. Registration is fast and FREE... Join Now!




Sharing variables between classes

 
Reply to this topicStart new topic

Sharing variables between classes

nombre
20 Jun, 2008 - 09:52 PM
Post #1

New D.I.C Head
*

Joined: 7 Jun, 2008
Posts: 49

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?
User is offlineProfile CardPM
+Quote Post

rgfirefly24
RE: Sharing Variables Between Classes
21 Jun, 2008 - 03:38 AM
Post #2

D.I.C Regular
Group Icon

Joined: 7 Apr, 2008
Posts: 335



Thanked: 5 times
Dream Kudos: 150
My Contributions
QUOTE(nombre @ 20 Jun, 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?


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:

java

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:

java

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


User is offlineProfile CardPM
+Quote Post

nombre
RE: Sharing Variables Between Classes
21 Jun, 2008 - 01:18 PM
Post #3

New D.I.C Head
*

Joined: 7 Jun, 2008
Posts: 49

rgfirefly24 does your example give the variable newvariable the value of classone.variableone at the end of the class or the beginning?
User is offlineProfile CardPM
+Quote Post

rgfirefly24
RE: Sharing Variables Between Classes
21 Jun, 2008 - 03:19 PM
Post #4

D.I.C Regular
Group Icon

Joined: 7 Apr, 2008
Posts: 335



Thanked: 5 times
Dream Kudos: 150
My Contributions
the way its shown it does it at the beginning, but you can do it anywhere throughout the code of classtwo.
User is offlineProfile CardPM
+Quote Post

neotrumatrix
RE: Sharing Variables Between Classes
21 Jun, 2008 - 06:25 PM
Post #5

New D.I.C Head
Group Icon

Joined: 6 Oct, 2005
Posts: 49



Thanked: 3 times
Dream Kudos: 50
My Contributions
QUOTE(nombre @ 21 Jun, 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?


Firstly it wouldn't assign the variable at all not without the Main method wink2.gif ...

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
User is offlineProfile CardPM
+Quote Post

nombre
RE: Sharing Variables Between Classes
21 Jun, 2008 - 11:15 PM
Post #6

New D.I.C Head
*

Joined: 7 Jun, 2008
Posts: 49

Never mind. Sorry about taking up boxes.

This post has been edited by nombre: 21 Jun, 2008 - 11:59 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:30AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month