• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Comparing Globalized Variables?

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to Compare golbalized variables.. ie use them in If/Else Statements??
for example i have globalized some variables using the flowing class
class Global
{
public static String name1 ;
public static String name2 ;

}

And later on in other classes when i call these local classes
eg. later on i used the folling statements in anther class....
if(Global.name1==Global.name2)
{
System.out.println("same");
}
else
{
System.out.println("diff");
}
Where Global.name1 and Global.name2 were defined in two other totally seperate classes as "
Global.name1 = "hello";
and
Global.name1 = "hello";
note that they are the same strings.. they have the same value
..and when the program loads it prints "diff" becasue of the if and else statements shown before. They never print same..why is this?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Avin

This has been discussed many times before.
You need to do a search in either the programmers cert forummor the Java in general Beginner forum for an explaination of == vs equals(). Or look them up in the String API.
Also, I've never seen the approach you use to create global variables. It is my understanding (and I could be wrong) that if you need to go to these lengths to create a 'global' variable then it isn't truely object oriented, and you might want to take a look at your class design. someone a little more experienced than me can probably add to this...

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
reply
    Bookmark Topic Watch Topic
  • New Topic