aspose file tools*
The moose likes Beginning Java and the fly likes help please... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help please..." Watch "help please..." New topic
Author

help please...

Lawrence Buensalida
Ranch Hand

Joined: May 18, 2005
Posts: 38
This may sound easy to you that's why I need your help. How do I use a string value from one class to another class?

Example: I have a string in class A and I want to use it for a decision in class B. How do I do that? Will you help me?

Thanks.
Rahul Juneja
Ranch Hand

Joined: Aug 03, 2002
Posts: 425
Either Extending that class and using its public and protected variables or if you if the conditions permit then make the varible as public static and then access the values.

Lawrence, if you give the complete code or scenario why you require, then I would be able to help you more.

Cheers,
Rahul


Rahul Juneja
ThoughtClicks - http://techlabs.thoughtclicks.com
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Most commonly, you pass the String as a method argument to the other class.

Let's say main() is in class A. You want to run some code in class B. The only way to do that is to call a method, right?



Alternatively, the variable may be in B, and A wants it. So you add a method to B that returns the desired variable when called:




Some variation on one of these is almost certainly what you're looking for.


[Jess in Action][AskingGoodQuestions]
Lawrence Buensalida
Ranch Hand

Joined: May 18, 2005
Posts: 38
Rahul,

I will post the code so that I may help you help me. Thanks.
Lawrence Buensalida
Ranch Hand

Joined: May 18, 2005
Posts: 38
Ernest,

Thanks for the code tip, I will try to apply to my source code and I'll see what I can do from there. Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Also see Stuart Grey's answer here. Looks like you posted this twice by mistake.
Stuart Gray
Ranch Hand

Joined: Apr 21, 2005
Posts: 410
Thanks Ernest. I realised there was a dupe and tried to delete my post, but the thread was locked before I got the chance.
Lawrence Buensalida
Ranch Hand

Joined: May 18, 2005
Posts: 38
Stuart,

I'm sorry, but I read your reply. When I was adding a post, I cliked two buttons, one below this message box and the other below the this page. I was trying to delete the post. Thanks for the reply.
[ June 10, 2005: Message edited by: Lawrence Buensalida ]
Lawrence Buensalida
Ranch Hand

Joined: May 18, 2005
Posts: 38
Guys, here's what I did...

I have here class "New Game"

public class NewGame
{
private String GameLevel;

public void Level(int level)
{
switch(level)
{
case 0: GameLevel = "EASY";
break;
case 1: GameLevel = "MEDIUM";
break;
case 2: GameLevel = "HARD";
break;
default:GameLevel = "EASY";
break;
}

}

public String getLevel()
{
return GameLevel;
}

}
=========================================================
I have here class A that calls Level from NewGame:

public class A
{
NewGame newGame = new NewGame();

public A
{
//....I have some codes here
//...I will call EasyRound()
}

private void EasyRound()
{
newGame.Level(0); //I will pass a value of zero to NewGame Level
}
}

=========================================================
I have another class, class B that needs the GameLevel string value:

public class B
{
NewGame newGame = new NewGame();
String gameLevel = newGame.getLevel();

public B
{
//...some code here
}

private void scnNewGame()
{
// here is where I want to see the string.
System.out.println(gameLevel);

}
}

=================================================

Here's the problem, I keep getting "null" instead of "EASY", "MEDIUM", or "HARD". It's obvious that there's something wrong (because I'm not getting my results... ) Help me please, thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

There are two different "NewGame" objects: the one you're creating from A, and the one you're creating from B. Your A instance and your B instance need to share a common NewGame object. That might look like this:

class Game {
public static void main(String[] argv) {
NewGame game = new NewGame();
A a = new A(game);
B b = new B(game);
// More...
}
}

class A {
private NewGame newGame;
A(NewGame game) {
newGame = game;
}
// More
}

class B {
private NewGame newGame;
B(NewGame game) {
newGame = game;
}
// More
}
[/code]

Now your "A" and your "B" are sharing a single NewGame object. Changes you make to the NewGame from "A" will be reflected in "B". Make sense?
Lawrence Buensalida
Ranch Hand

Joined: May 18, 2005
Posts: 38
Ernest,

Thanks for the reply and I think I understand. But I forgot to mention that NewGame, A, and B are 3 different files. Would that still be ok?
 
Don't get me started about those stupid light bulbs.
 
subject: help please...