Results 1 to 4 of 4
  1. #1
    tornado is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default Newbie help on visibility and static final

    Hi,
    I have a class where all my constants are defined,

    Is there any way I can have the constants as:
    private static final

    and still be able to change the values from other classes?

    So lets say in myconstant.java I have:

    private static final int MY_CONSTANT = 5;

    inside myclass.java I want to be able to access:

    if(myconstant.MY_CONSTANT > 10)

    and be able to change like:

    myconstant.MY_CONSTANT = 10;

    The problem is if I want to access outside class I have to specify protected or public.
    And I have to get rid of the final keyword if I want to be able to change the constant value in other classes.

  2. #2
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default

    Quote Originally Posted by tornado View Post
    Hi,
    I have a class where all my constants are defined,

    Is there any way I can have the constants as:
    private static final

    and still be able to change the values from other classes?

    So lets say in myconstant.java I have:

    private static final int MY_CONSTANT = 5;

    inside myclass.java I want to be able to access:

    if(myconstant.MY_CONSTANT > 10)

    and be able to change like:

    myconstant.MY_CONSTANT = 10;

    The problem is if I want to access outside class I have to specify protected or public.
    And I have to get rid of the final keyword if I want to be able to change the constant value in other classes.
    Use final if they are constants. If they are not, don't! For example:

    Java Code:
    private static int MY_CONSTANT = 5;

  3. #3
    tornado is offline Member
    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Default

    OK then I will leave final, but I wanted to use it, because the user enters these values in from the GUI and from then on they are final.

    How about the visiblity modifier.
    I would like to keep it private but then I cannot access the constant from another class.
    Is there anyway to do this, or do I have to specify public? It lets me used protected, but don't know what this means.

  4. #4
    racerxadam is offline Member
    Join Date
    Jul 2008
    Posts
    68
    Rep Power
    0

    Default

    You can create a constants object and have getter/setter methods on that object to modify them. It doesn't really sound like you want constants in the true sense of the word. A constant is just that, constant for every instance of running your program. It sounds more like you want a data carrier for user input.
    Java Code:
    public class UserInputDataCarrier {
    
    	private int propertyOne = 0;
    	private String propertyTwo = null;
    
    	public UserInputDataCarrier() {
    
    	}
    
    	public int getPropertyOne() {
    
    		return propertyOne;
    	}
    
    	public void setPropertyOne( int propertyOne ) {
    
    		this.propertyOne = propertyOne;
    	}
    
    	public String getPropertyTwo() {
    
    		return propertyTwo;
    	}
    
    	public void setPropertyTwo( String propertyTwo ) {
    
    		this.propertyTwo = propertyTwo;
    	}
    }

Similar Threads

  1. static final field
    By techie.it19 in forum New To Java
    Replies: 3
    Last Post: 10-16-2008, 04:12 AM
  2. Newbie question about Static methods
    By SCS17 in forum New To Java
    Replies: 9
    Last Post: 02-06-2008, 08:03 AM
  3. Final,abstract and static
    By $hr!k@nt in forum Advanced Java
    Replies: 5
    Last Post: 01-09-2008, 05:23 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
  •