Re: Basic array question, need help
From: KellyH (Kelly_at_whatever.com)
Date: 12/12/03
- Previous message: RadioFreq: "Re: Retreiving Server Time"
- In reply to: Karl von Laudermann: "Re: Basic array question, need help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 12 Dec 2003 02:21:47 GMT
Thanks everyone for your help! I didn't realize I could instantiate the
array in the constructor. Got it working now. Moving on to other
problems...
--
-Kelly
kelly at farringtons dot net
Check out www.snittens.com
"Karl von Laudermann" <karl@ueidaq.com> wrote in message
news:e7e6125e.0312100843.1f8741bf@posting.google.com...
> "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();
> }
- Previous message: RadioFreq: "Re: Retreiving Server Time"
- In reply to: Karl von Laudermann: "Re: Basic array question, need help"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]