java - When to use a constant? -



java - When to use a constant? -

below code snippet of arraylist, either takes in 'initialcapacity' provided user or default of 10. question - why 10 not static constant ? guess constants used when variable repeated more 1 time in code. right ?

public arraylist(int initialcapacity) { super(); if (initialcapacity < 0) throw new illegalargumentexception("illegal capacity: "+ initialcapacity); this.elementdata = new object[initialcapacity]; } /** * constructs empty list initial capacity of ten. */ public arraylist() { this(10); }

constants used give names variables.

you phone call "default_initial_capacity"

a program, 1 can read story.

here code snippet example:

char * find_type(char *str,int *type){ str = trim_leading_space(str); if(*str == slash_sign){ str++; }else{ display_error(bad_type_format, str); } str = trim_leading_space(str); *type = convert_to_zero_or_one(str[0]); if(*type == no){ display_error(bad_type_format, str); }else { str++; } homecoming str; }

the code written in c, still proves point.

here definition of "bad_type_format"

#define bad_type_format 34

if had left 34, when came edit code, wouldn't know why wrote 34 in it. next index of error, long list of possible errors.

java constants

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -