This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
How to make a Variable public?
Posted by Avin Sinanan on November 28, 2001 at 2:16 AM
Does anyone know how to make a variable public to all classes? take for example the code>.. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.*; class Compare { public static void main(String[] args) { JFrame frame = new JFrame("One"); String name1 ; String name2 ; } }
class Class1 { public String method1() { name1= "Rick" ; return name1; } } class Class2 { public String method2() { name2 = "Jenny"; return name2; } } does anyone know how to make classses Class1 and Class2 recognise the Strings name1 and name2 without adding a variables to Class1 and Class2. IS there anyway to make variables golbally accessable.. so that it can be recognised anywehere within a program..even within other classes? And please don't tell me to delcare String name1 and String name2 within Class1 and Class2 respectively. I really need for my variables to be declared within the public class. thanks...
Replies:
|