Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Returning or printing the name of a constant

  1. #1
    Join Date
    Oct 2011
    Posts
    1
    Rep Power
    0

    Default Returning or printing the name of a constant

    Hi,

    if i have a constant, which is a part of my object, and then i have a method that prints out all the information of that object, how do i get it to print the NAME of the constant rather than the VALUE.

    Here is a small relevant part of my program (this is only the default constructor):

    Java Code:
    class Fan
    {
    	final static int SLOW = 1;
    	final static int MEDIUM = 2;
    	final static int FAST = 3;
    	
    	int speed;
    	boolean on;
    	double radius;
    	String color;
    	
    	public Fan()
    	{
    		speed = SLOW;
    		on = false;
    		radius = 5;
    		color = "blue";
    	
    	}
    
    ...
    And here is the method:

    Java Code:
    public String toString()
    	{
    		String info = "";
    		
    		if( getOn() == true)
    		{
    			 info = "The fan speed is: " +getSpeed() +", the radius is: " +getRadius() +", the color is: " + getColor();
    			
    		}
    		else
    		{
    			 info = "The color is: " +getColor() + ", the radius is: " +getRadius() +", the fan is OFF.";
    			
    		}
    		
    		return info;
    		
    		
    	}
    The thing that you need to pay attention to is the getSpeed()

    When i print out this program i get "the speed is 1" when instead i want to get "the speed is SLOW" (SLOW being the name of the constant)

    If you like i can post my whole program, but this is just the relevant bits

    Thank you very much

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

    Default Re: Returning or printing the name of a constant

    You have two choices as I see it:

    1. Don't use int constants but instead use String constants. Have SLOW = "Slow", FAST = "Fast", ...
    2. Use an enum instead which has a toString() method that can return the enum's name. You can give it a private int value field if you need an int value to be tied with it.
    sunde887 likes this.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Returning or printing the name of a constant

    Fubarable beat me to it, but the enum idea is useful for situations such as these.

Similar Threads

  1. Constant Variables
    By harshakantha in forum New To Java
    Replies: 3
    Last Post: 06-13-2011, 08:51 AM
  2. constant variables questions
    By sgthale in forum New To Java
    Replies: 3
    Last Post: 05-06-2011, 06:34 AM
  3. switch() don't work with constant
    By jarek in forum Advanced Java
    Replies: 3
    Last Post: 11-19-2010, 09:59 AM
  4. static constant and performance
    By felixtfelix in forum New To Java
    Replies: 2
    Last Post: 08-31-2010, 02:43 PM
  5. Constant size for JTextArea
    By itaipee in forum AWT / Swing
    Replies: 1
    Last Post: 12-30-2008, 05:12 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
  •