Newbie question

E

eduardo.rosa

Hi people,
I've a question about scope in java, somebody can help me?
How can access 's' variable in code below?

thanks a lot.

public class TopClass
{
protected String s;

public TopClass()
{
AnyClass anyclass = new AnyClass() {
//methodThatINeedToImplement is abstract method from
AnyClass

void methodThatINeedToImplement()
{
//how can I acess 's' variable here?
}
}
}
 
T

Thomas Hawtin

I've a question about scope in java, somebody can help me?
How can access 's' variable in code below?

thanks a lot.

public class TopClass
{
protected String s;

private is a better choice.
public TopClass()
{
AnyClass anyclass = new AnyClass() {
//methodThatINeedToImplement is abstract method from
AnyClass

void methodThatINeedToImplement()
{
//how can I acess 's' variable here? s = "fred";
} };
}
}

You may have a problem if you read s before assigning it a value (it
will still be null).

Tom Hawtin
 
O

Oliver Wong

Hi people,
I've a question about scope in java, somebody can help me?
How can access 's' variable in code below?

thanks a lot.

public class TopClass
{
protected String s;

public TopClass()
{
AnyClass anyclass = new AnyClass() {
//methodThatINeedToImplement is abstract method from
AnyClass

void methodThatINeedToImplement()
{
//how can I acess 's' variable here?
}
}
}

You shouldn't have any problem accessing s there. That being said, it
looks like you're accidentally declaring nested classes. Instead of:

<code>
public class TopClass {
public class TopClass {
/*whatever*/
}
}
</code>

did you perhaps mean:

<code>
public class TopClass {
/*whatever*/
}
</code>

- Oliver
 
M

Matt Humphrey

Hi people,
I've a question about scope in java, somebody can help me?
How can access 's' variable in code below?

thanks a lot.

public class TopClass
{
protected String s;

public TopClass()
{
AnyClass anyclass = new AnyClass() {
//methodThatINeedToImplement is abstract method from
AnyClass

void methodThatINeedToImplement()
{
//how can I acess 's' variable here?
}
}
}

This is the kind of test that's easy for you to check yourself as it pretty
much compiles as-is. Much more fun and educational than asking for answer.
It works because the scope of an inner class includes the outer class.
Every inner class includes a reference to the enclosing outer class. Your
new class, which is an anonymous inner subclass of AnyClass, will have
access to TopClass's instance variables, including s, as well as its
methods. It makes no difference whether AnyClass is a real class or an
abstract class or an interface or whether it has concrete or abstract
methods--only that it's an inner class.

The following compiles and runs just fine. I've changed the protection of s
to private from protected to show that it is within class scope and I've
extended the code so that it's clear that the inner class can incorporate s
before s has a value and that the value of s may change. (This isn't
necessarily good style--it's just to show how it works.)

public class TopClass {
private String s;
private AnyClass anyclass;

public TopClass() {
anyclass = new AnyClass() {
public void methodThatINeedToImplement() {
System.out.println(s);
}
};
}
public void setS (String v) { s = v; }
public void doX () { anyclass.methodThatINeedToImplement (); }

public static final void main(String[] args) {
TopClass tc = new TopClass ();
tc.setS ("First");
tc.doX ();
tc.setS ("Second");
tc.doX ();
}
}

public abstract class AnyClass {
public abstract void methodThatINeedToImplement ();
}

Local variables present at the time the inner class is instantiated are also
in scope, but must be marked "final" so as to clarify the fact that their
references are copied to the new instance and cannot change.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
J

Jussi Piitulainen

Hi people,
I've a question about scope in java, somebody can help me?
How can access 's' variable in code below?

Just reference it. Here is one answer I found by searching the web, at
<http://java.sun.com/developer/Books/certification/page5.html>:

# Inner classes, unless static, have access to the variables of the
# enclosing class instance. Additionally, inner classes defined in
# method scope have read access to final variables of the enclosing
# method.

Your s is a variable of the enclosing instance of TopClass.

If you have problems, they are elsewhere: inability to refer to
anyclass outside TopClass constructor, maybe, or maybe a missing
semicolon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top