Results 1 to 7 of 7
  1. #1
    mike1127 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default variables/data not in classes?

    I'm very new to Java. I'm used to putting variables and data definitions at the top level in a module; but I get syntax errors doing that in Java? Is there some standard way to provide data that's in a module or namespace but not part of a class?
    Thanks,
    Mike

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: variables/data not in classes?

    Everything *must* be in a class or similar top-level construct such as enum or interface.

  3. #3
    mike1127 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: variables/data not in classes?

    Thanks. I have another question. Is the one-class-per-file restriction something in Java itself, or is that something Eclipse is forcing on me?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: variables/data not in classes?

    It's a Java restriction. Each file can have only one public top-level class, and that class must have the same name as the file.

  5. #5
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: variables/data not in classes?

    And I think it is better not to use so many global variables in your code. Consider to use local variables more. If you have a large method, breaks it into a smaller chunk of code.

  6. #6
    mike1127 is offline Member
    Join Date
    Mar 2013
    Posts
    6
    Rep Power
    0

    Default Re: variables/data not in classes?

    Quote Originally Posted by wsaryada View Post
    And I think it is better not to use so many global variables in your code. Consider to use local variables more. If you have a large method, breaks it into a smaller chunk of code.
    That's true, but I guess I'm coming from Haskell, which makes finer differentiations with the concepts of namespace, class, data, instance, and type, so it feels weird to me to make a class for something that there will only be one of. That's what I was thinking---there's only going to be one set of these variables. I guess in OO that's called the "singleton pattern"?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default Re: variables/data not in classes?

    Quote Originally Posted by mike1127 View Post
    That's true, but I guess I'm coming from Haskell, which makes finer differentiations with the concepts of namespace, class, data, instance, and type, so it feels weird to me to make a class for something that there will only be one of. That's what I was thinking---there's only going to be one set of these variables. I guess in OO that's called the "singleton pattern"?
    The singleton pattern is just a small pattern (and over estimated); if you're absolutely, positively, definitely sure that you only need one 'thing' you can get away with a 'utility class', i.e. a class with just static methods (public) and optional static data (private); the class serves as a name space and nothing else. Java complicates matters a bit because the same class can be loaded by another class loader and you'll end up with more singleton objects or singleton utility classes ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help with instances of classes and variables!
    By lifedistroy in forum New To Java
    Replies: 5
    Last Post: 12-24-2012, 08:15 AM
  2. Passing variables through classes?
    By EternalFacepalm in forum New To Java
    Replies: 4
    Last Post: 04-27-2011, 01:46 AM
  3. Replies: 4
    Last Post: 02-07-2011, 08:38 PM
  4. sending variables to different classes?
    By Erdrick in forum New To Java
    Replies: 9
    Last Post: 08-04-2008, 12:11 PM
  5. [SOLVED] Need help using variables between classes
    By Renegade85 in forum New To Java
    Replies: 4
    Last Post: 04-22-2008, 03:53 PM

Posting Permissions

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