-2
public class Test2 {
    public static void main(String[] args) {

         if(args.length < 2) {
             System.err.println("Error !!");
             System.exit(1);    
        }
          float distance = Float.parseFloat(args[0]);
          float time = Float.parseFloat(args[1]);

          System.out.print("Velocity = " );
          System.out.print(distance / time);
          System.out.print(" m/s ");
        }   
}

How can I make variables constant?
Constant distance = 10;
Constant time = 5;

Thank Everyone!

Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
aca
  • 25
  • 3
  • 2
    Add `final` to the variable declaration: `final float distance = ...` – Luiggi Mendoza Mar 14 '13 at 14:55
  • 4
    Please explain what you're trying to achieve. As it stands, the question is rather cryptic. – NPE Mar 14 '13 at 14:56
  • @NPE I actually think it is quite clear what the OP is asking. What's cryptic about it? – Kakalokia Mar 14 '13 at 14:57
  • if you're using the constants in static methods, also add the reserved word static: final static float distance = 10; See the answer above. – Marcelo Tataje Mar 14 '13 at 14:57
  • 2
    @AliAlamiri: Well I certainly don't understand "How do I need to set constants?" - what do you understand by that question? – Jon Skeet Mar 14 '13 at 14:58
  • 3
    Wow that was a fast closure. 4 minutes? Give the guy a chance to explain! – Boann Mar 14 '13 at 14:58
  • @JonSkeet It could be a problem that his English is not very good, and he couldn't express the question of "How do I make variables constant" in a better way. – Kakalokia Mar 14 '13 at 15:00
  • 1
    @Boann - Agreed, it's actually a bit harsh. sorry op – Caffeinated Mar 14 '13 at 15:00
  • The question definitely doesn't need to be closed. – Kakalokia Mar 14 '13 at 15:03
  • @AliAlamiri: Yes, I'm sure it's that his English isn't very good. But that doesn't make it any clearer. What is the "No change source code" bit about? Given that the code already prints the distance and time, what's really left to change? *Why* does the OP want constants? – Jon Skeet Mar 14 '13 at 15:03
  • @AliAlamiri: Until the OP edits it to actually make it a *clear* question, I completely agree with it being closed as "not a real question". – Jon Skeet Mar 14 '13 at 15:04

4 Answers4

4
final float distance = Float.parseFloat(args[0]);
final float time = Float.parseFloat(args[1]);
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
2

YOu can try something liek this:-

public static final int x= 11;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

Just declare them.

  public class Test2 {
        public static final float distance = 10;
        public static final float time = 5;
        public static void main(String[] args) {

             if(args.length < 2) {
                 System.err.println("Error !!");
                 System.exit(1);    
            }
              float distance = Float.parseFloat(args[0]);
              float time = Float.parseFloat(args[1]);

              System.out.print("Velocity = " );
              System.out.print(distance / time);
              System.out.print(" m/s ");
            }   
    }
Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
Marcelo Tataje
  • 3,849
  • 1
  • 26
  • 51
0

This is done using the final keyword

public static final int distance = 10

then you can just use the distance wherever you need 10 ( src )

also see: private final static attribute vs private final attribute

Community
  • 1
  • 1
Caffeinated
  • 11,982
  • 40
  • 122
  • 216