For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2006 > Re: Have a constructer with the paramters sent through an applet store them as global









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Have a constructer with the paramters sent through an applet store them as global
Nigel Wade

2006-03-24, 8:03 am

Richard Perreault wrote:

> I have a program that uses an applet to transfer parameters from an Html
> page. And transfer them to a constructor. This works very well, I did add
> println commands to make sure it functions properly. When I open the applet
> which loads a frame. I see the variables even before the frame is loaded. My
> problem is that these variables are not global. If I do not create the
> variable at the beginning of the Frame I get an error as symbol not found.
> Here is some snippets of my program.
>
> This is the applet
> Url = getParameter("Url");


It would help enormously if you adhered to the convention that variable names
begin with a lowercase letter and class names with uppercase. When I first saw
the above line I thought it was totally incorrect because you were attempting
to assign a String to a Url.

In newsgroups communication is everything, and failing to communicate
successfully means that you won't get the help you want. Making our life easier
by using the conventions is more likely to get help, rather than being ignored.


> HTTP = getParameter("HTTP");
> System.out.println(Url);
> System.out.println(HTTP);
>
> //try {
> //finalapp being loaded
> //This is how you pass parameters from an applet to a frame!
> window = new finalapp (Url,HTTP,"Pop-Up Window");
> //catch (IOException e) {
> //System.out.println("Error: "+ e.getMessage());
> //}
>
> And this is the constructor of the Frame
>
> // Constructor
> public finalapp(String Url,String HTTP,String title)
> {
> super(title);
> System.out.println(Url+" "+HTTP);
> // Read the Questions file;
> // Frame Initialization
>
> Can anyone please tell me how to make the Url and HTTP global variables in
> the Frame.


A variable which is "global" to a class is a class variable (static) in that it
is shared amongst all instances of that class. But I don't think that's what
you want here. I think what you want is a variable which is accessible by all
methods within the class. For that you want an instance variable. These are
defined in the class, but outside of any method. They can be set within the
constructor.

You can't make a variable which is a parameter to the constructor visible to
other methods, you have to declare a variable in the scope of the class and set
it in the constructor.

e.g.

public class FinalApp {
String url;
String http;

public FinalApp(String Url,String HTTP,String title) {
...
url = Url;
http = HTTP;
...
}

public void someMethod() {
System.out.println(url);
}
....


--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail : nmw@ion.le.ac.uk
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
Richard Perreault

2006-03-24, 7:10 pm

Thank you that work perfectly.

"Nigel Wade" <nmw@ion.le.ac.uk> wrote in message
news:e00h9g$5o6$1@south.jnrs.ja.net...
> Richard Perreault wrote:
>
>
> It would help enormously if you adhered to the convention that variable
> names
> begin with a lowercase letter and class names with uppercase. When I first
> saw
> the above line I thought it was totally incorrect because you were
> attempting
> to assign a String to a Url.
>
> In newsgroups communication is everything, and failing to communicate
> successfully means that you won't get the help you want. Making our life
> easier
> by using the conventions is more likely to get help, rather than being
> ignored.
>
>
>
> A variable which is "global" to a class is a class variable (static) in
> that it
> is shared amongst all instances of that class. But I don't think that's
> what
> you want here. I think what you want is a variable which is accessible by
> all
> methods within the class. For that you want an instance variable. These
> are
> defined in the class, but outside of any method. They can be set within
> the
> constructor.
>
> You can't make a variable which is a parameter to the constructor visible
> to
> other methods, you have to declare a variable in the scope of the class
> and set
> it in the constructor.
>
> e.g.
>
> public class FinalApp {
> String url;
> String http;
>
> public FinalApp(String Url,String HTTP,String title) {
> ...
> url = Url;
> http = HTTP;
> ...
> }
>
> public void someMethod() {
> System.out.println(url);
> }
> ...
>
>
> --
> Nigel Wade, System Administrator, Space Plasma Physics Group,
> University of Leicester, Leicester, LE1 7RH, UK
> E-mail : nmw@ion.le.ac.uk
> Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555



Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com