Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Newbie question

Thread view: 
eduardo.rosa@gmail.com - 15 Jun 2006 18:43 GMT
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?
                 }
     }
}
Thomas Hawtin - 15 Jun 2006 19:18 GMT
> I've a question about scope in java, somebody can help me?
> How can access 's' variable in code below?
[quoted text clipped - 4 lines]
> {
>       protected String s;

private is a better choice.

>       public TopClass()
>       {
[quoted text clipped - 5 lines]
>                   {
>                         //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
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/

Oliver Wong - 15 Jun 2006 19:30 GMT
> Hi people,
> I've a question about scope in java, somebody can help me?
[quoted text clipped - 18 lines]
>      }
> }

   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
Matt Humphrey - 15 Jun 2006 19:39 GMT
> Hi people,
> I've a question about scope in java, somebody can help me?
[quoted text clipped - 18 lines]
>      }
> }

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 matth@ivizNOSPAM.com  http://www.iviz.com/
Jussi Piitulainen - 15 Jun 2006 19:47 GMT
> 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.

> thanks a lot.
>
[quoted text clipped - 14 lines]
>       }
> }
eduardo.rosa@gmail.com - 16 Jun 2006 18:06 GMT
Thanks a lot people !


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.