Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help please...

 
Lawrence Buensalida
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lawrence Buensalida
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul,

I will post the code so that I may help you help me. Thanks.
 
Lawrence Buensalida
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also see Stuart Grey's answer here. Looks like you posted this twice by mistake.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic