SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Java quickie

  1. #1
    SitePoint Addict Drinky's Avatar
    Join Date
    Jan 2001
    Location
    England
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want to assign a meaningful name to true and false in Java (this is my first attempt at doing anything in java, i know the answer must be dead simple ). The class they are used in is as follows:
    Code:
    public class Door extends Object 
    {
    	//const definition
    	const boolean open = true;
    	const boolean closed = false;
    		    
    	//Private variables
    	private boolean OpenClosedStatus;
    	
    	//Default Constructor - Creates a Door
    	public Door ()
    	{
    		this.OpenClosedStatus = closed;
    	}
    	
    	public void open()
    	{
    		this.OpenClosedStatus = open;
    	}
    
    	public void close()
    	{
    		this.OpenClosedStatus = closed;
    	}
    }
    Visual J++ says the two statements at the top are missing identifiers.

    In C++ you would do something like:
    #Define OPEN = true

    Don't pick holes in the fact that true is not a valid c++ statement.

    I have looked through 3 java books and the MSDN CD and can't find a single code example or explaination for this problem.
    Drinky

  2. #2
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm just learning Java as well, but I'll try to guess. As far as I remember from my book, 0==false and 1==true.

    //const definition
    const boolean open = 1;
    const boolean closed = 0;

    Maybe that would work...

  3. #3
    SitePoint Addict Drinky's Avatar
    Join Date
    Jan 2001
    Location
    England
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh but you see, true and false are valid values in java, what i'm getting at is that there is i'm getting an error on the const declaration line.
    Drinky

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You sure "const" is what you're looking for? The big blue book I have with me has constants in this format:

    final boolean open = true;
    final boolean closed = false;


    Let me know if that works...

    EDIT: I think it's common practice to name constants with all capital letters...IE: "OPEN" and "FALSE" - your choice of course, but that's the way I usually do it.

  5. #5
    SitePoint Addict Drinky's Avatar
    Join Date
    Jan 2001
    Location
    England
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well i'm not sure if const will let me do what i want in java but in c++ it's #Define and in all the other languages i use you can define constants.

    I'll give it a try and see if it works cheers. I'll let you know.
    Drinky

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, sounds cool. Just do me one favor: don't post any C/C++ code here...my head hurts enough as it is. And yeah, I think I semi-understand what you mean. In PHP you use the word "define" as well:

    DEFINE(CONSTNAME, "Constant Value");

    I did think it was maybe a little odd that Java used "final"...but each language has it's own little oddities like that.

  7. #7
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My friend Ravi just pointed something out: what is the value of a constant boolean value? The only thing that comes to mind is to allow you to disable a program, and enable it again easily.

  8. #8
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Milton Keynes, UK
    Posts
    1,011
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Drinky,

    Try the following:

    Code:
    //const definition
    static final boolean open = true;
    static final boolean closed = false;
    The static means the variable is a class variable.

    Shane




  9. #9
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think what Chris said "final boolean open = true;" is enough and correct.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  10. #10
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, that's what my big blue book says. What's the verdict, Drinky?

  11. #11
    SitePoint Addict Drinky's Avatar
    Join Date
    Jan 2001
    Location
    England
    Posts
    338
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry guys forgot to mention that

    final boolean open = true;
    final boolean closed = false;

    fit the bill perfectly

    static final boolean open = true;
    static final boolean closed = false;

    these also work

    cheers guys


    Drinky

  12. #12
    SitePoint Zealot
    Join Date
    Nov 2000
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the one thing that is confusing me, is why would you have a boolean expression for a varialbe if it is open and one if it is closed


    final boolean open = true;
    final boolean closed = false;

    (or what ever you typed above)

    wouldn't you only want one.

    final boolean open = false;

    if it is "TRUE" then the object is open if it is "FALSE" the object is closed.

    Just putting my two sence in.

  13. #13
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know this is slightly off-topic Drinky, but in C++ prefer the use of const over # define as in

    const int MY_INT = 3;

    is better than

    # DEFINE MY_INT 3

    Why? Because:

    # define is a pre-processor directive. It isn't really part of your code. The pre-processor, parses your code and views MY_INT as a "macro" substituting 3 where-ever it sees MY_INT in your code.

    As with so much depricated C, it is far less safe than the superior C++ alternative. Here are some problems:

    1) It's not type safe, which is far more important when you are compiling code. ie, if you say

    # DEFINE MY_INT catfish

    the compilor is not going to necesarrily complain. but if you say

    const int MY_INT = catfish;

    you bet its gonna complain

    2) You may accidently redefine MY_INT and give it a different value in different files (it would be silly - but that just means it more probable that it will happen ) However, if somewhere else you try to redefine the const

    const int MY_INT = 5;

    within the same namespace the compilor will alert you to your folly.


Bookmarks

Posting Permissions

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