Re: Basic array question, need help
From: Karl von Laudermann (karl_at_ueidaq.com)
Date: 12/10/03
- Next message: YesBalala: "JRE 1.4.2_02 hsperfdata_SYSTEM"
- Previous message: Peter Chatterton: "Where's the logon password in Apache?"
- In reply to: KellyH: "Basic array question, need help"
- Next in thread: KellyH: "Re: Basic array question, need help"
- Reply: KellyH: "Re: Basic array question, need help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 10 Dec 2003 08:43:18 -0800
"KellyH" <Kelly@whatever.com> wrote in message news:<UqoBb.484596$Tr4.1330037@attbi_s03>...
> Here's my problem:
> My problem is that I can't figure out how to load the array. If I do it in
> the Enter button event, then a new array is created each time an integer is
> entered. I tested this by adding the current int to the int in the previous
> cell. Oh, I am also storing the logical end of the array in the 0 cell. If
> I try to load the array in the main, I get an error: Non-static method
> load_aNum cannot be referenced from a static context. We had a project
> previously where I used two arrays (pre-filled, not using input numbers),
> and I loaded it in the main, and it worked fine. Don't know why this won't
> work.
I assume by "load" you mean allocate and initialize. The correct place
to do it is in the constructor, since that's exactly what constructors
are for:
/** Creates a new instance of khNumberDisplayer */
public khNumberDisplayer()
{
// Allocate and initialize array
aNum = new int[50];
aNum[0]++;
setTitle ("Number Displayer by Kelly");
setLayout (new FlowLayout());
add (new Label ("NUMBER "));
add (tfNum1);
add (new Label ("Message = "));
add (taMessage);
btnEnter = new Button ("Enter");
add (btnEnter);
btnEnter.addActionListener (new EnterHandler(this));
addWindowListener(new CloseWindow());
}
The reason you can't access aNum from main is because main is a static
method, which means it belongs to the khNumberDisplayer class in
general, not any particular instance of it. Static methods cannot
access non-static member variables, since static methods can be
executed even when no instances of the class exist. If you really
wanted to allocate the array from main rather than the constructor
(though there's no good reason to), you could do it this way:
public static void main (String []args)
{
Frame NumberDisplayerWindow = new khNumberDisplayer();
NumberDisplayerWindow.setSize (700,300);
NumberDisplayerWindow.setBackground(Color.pink);
NumberDisplayerWindow.show();
// Call load_aNum on the instance
NumberDisplayerWindow.load_aNum();
}
- Next message: YesBalala: "JRE 1.4.2_02 hsperfdata_SYSTEM"
- Previous message: Peter Chatterton: "Where's the logon password in Apache?"
- In reply to: KellyH: "Basic array question, need help"
- Next in thread: KellyH: "Re: Basic array question, need help"
- Reply: KellyH: "Re: Basic array question, need help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]