Results 1 to 9 of 9
  1. #1
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Does the following code compile or not?

    public class Application {
    public static void main(String[] args) {

    final class Constants {
    public static String name = "PI";
    }

    Thread thread = new Thread(new Runnable() {

    @Override
    public void run() {
    System.out.println(Constants.name);
    }

    });



    thread.start();
    }
    }

    ANS : No.
    The problem here is that you can only use static variables in static or top-level classes, and we’ve got a static variable in a class that isn’t top-level.

    i m not getting it ... can you please explain ..

  2. #2
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Does the following code compile or not?

    There are many reasons why this code won't compile
    1. You cannot declare another class within a method ... class Constants
    2. For threads... your class will have to either extend Thread or implement Runnable

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Does the following code compile or not?

    Quote Originally Posted by Kagiso View Post
    There are many reasons why this code won't compile
    No, there's only one, and it's neither of the two you posted.

    Quote Originally Posted by Kagiso View Post
    1. You cannot declare another class within a method ... class Constants
    Did you try that and get an error, or are you merely guessing?

    Quote Originally Posted by Kagiso View Post
    2. For threads... your class will have to either extend Thread or implement Runnable
    There is an anonymous class that implements Runnable in the code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Does the following code compile or not?

    Quote Originally Posted by allaudin View Post
    you can only use static variables in static or top-level classes, and we’ve got a static variable in a class that isn’t top-level.

    i m not getting it ... can you please explain ..
    Read all about it: Chapter*8.*Classes

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Does the following code compile or not?

    Oops... My Bad....

    I now see the anonymous class... and I now see what's wrong with this code

    @ Darryl...THANKS FOR CORRECTING MY MISTAKE
    @ Allaudin ... Think constants
    Last edited by Kagiso; 06-23-2013 at 09:18 AM.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Does the following code compile or not?

    Quote Originally Posted by Kagiso View Post
    Oops... My Bad....

    I now see the anonymous class... and I now see what's wrong with this code

    @ Darryl...THANKS FOR CORRECTING MY MISTAKE
    You're welcome

    Quote Originally Posted by Kagiso View Post
    @ Allaudin ... Think constants
    Whatever for?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  7. #7
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Does the following code compile or not?

    Because Allaudin intended to declare a String constant named name

    Am I wrong again?

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Does the following code compile or not?

    Quote Originally Posted by Kagiso View Post
    Because Allaudin intended to declare a String constant named name

    Am I wrong again?
    Sorry, but you're wrong again.

    See Allaudin's paragraph I quoted at #4. That's the real question.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Does the following code compile or not?

    Extracted from the link you provided

    "Inner classes may not declare static members, unless they are constant variables, or a compile-time error occurs"

    That brings more light into these whole topic... THANKS
    But tell me what's wrong with turning variable name into a constant?

Similar Threads

  1. HELP!! Code won't compile - NullPointerException
    By JuliaDavies in forum NetBeans
    Replies: 10
    Last Post: 03-21-2012, 09:29 AM
  2. I cant get my code to compile Need help please
    By MikeJ39 in forum New To Java
    Replies: 6
    Last Post: 10-15-2011, 06:28 AM
  3. Can not compile code , anyone help me fix it ?
    By batista11b5 in forum New To Java
    Replies: 3
    Last Post: 10-22-2010, 02:00 PM
  4. Code does not Compile
    By ustar in forum New To Java
    Replies: 13
    Last Post: 03-06-2010, 04:54 PM
  5. Code will not compile
    By ShotGunRockets in forum New To Java
    Replies: 17
    Last Post: 05-10-2009, 03:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •