cancel
Showing results for 
Search instead for 
Did you mean: 

Basic Java class

Former Member
0 Kudos

Hi

I have the following java file. Can anyone explain me line by line, what each line does ?


public final class Period {
	
	private String period;
	private int id;

	public final static int ID_ONE = 1;

	public final static Period ONE = new Period("ONE", ID_ONE);

	private static HashMap periods = new HashMap(1, 1.0f);
	static {
		periods.put(ONE.toString(), ONE);
	}
	
	
	
	private Period(String period, int id) {
		this.period = period;
		this.id = id;
	}

                public int getId() {
		return this.id;
	}
	public static Period valueOf(String value) {
		return (Period) periods.get(value);
	}


}

Below is another program that uses this Java period file


Period period = Period.valueOf(<value>);
	switch (period.getId() )
{

     case Period.ID_ONE:
   ....
   ....

I am aware of constructors and HaspMap. But this piece of code is bit confusing.

Any Help ?

Regards,

Gandolf

Accepted Solutions (1)

Accepted Solutions (1)

former_member185029
Active Contributor
0 Kudos

Hello,

Let us go line by line

public final class Period {

//Public is an access specifier indicating that this class is accessible to all

//final indicates this class cannot be inherited further 

//class: keyword for defining class followed by classname 



	
	private String period;
//private: access specifier telling that this object is accessible to this class only.

	private int id;
//private: access specifier telling that this variable is accessible to this class only.

 
	public final static int ID_ONE = 1;
//public: access specifier telling that this object is accessible to all.
//final: the variable is constant and initialized to 1 

//I think name of this object should be ONE
	public final static Period ALL = new Period("ONE", ID_ONE);
//Try writting it as	
// public final static Period ONE= new Period("ONE", ID_ONE);

//This is the object of this class invoked with the constructor defined below
 
	private static HashMap periods = new HashMap(1, 1.0f);
//Defined hashmap with key value pair

	static {

//ONE.toString() for converting int into String

		periods.put(ONE.toString(), ONE);
	}
	
//static block for defining static code
	
	
//constructor with argument String and int
	private Period(String period, int id) {
		this.period = period;
                             //this.period is the class level object defined above

		this.id = id;
                             //this.id is the class level veriable


	}
 
                //Getter method for getting id variable
                public int getId() {
		return this.id;
	}

            //Defined a function valueOf with String argument and return type of Period
	public static Period valueOf(String value) {
//This statement gets value from the periods hashmap 
// object and typecast it into Period 
		return (Period) periods.get(value);
	}
 
 
}
 
\
Part 2 :Implimentation

//Created an object of Period by calling static method valueOf
//static methods can be called by using class name

Period period = Period.valueOf(<value>);

	switch (period.getId() )
{
 
     case Period.ID_ONE:
   ....
   ....

Answers (2)

Answers (2)

former_member193726
Active Participant
0 Kudos

Hi Gandolf,

I will try to explain what the code does...

(1) Initially the overloaded Period(String, int) constructor is called to create a Period object by name ONE.

(2) HashMap with initial size 1 and a loadfactor of 1.0f which is 100% memory overhead is created.

(3) To this HashMap the PERIOD entry is made with ONE.toString() as key and a value of ONE.

(4) The getId() method is to return the int id of the Period we are working on.

(5) The valueOf(String) returns the Period which corresponds to the HashMap key of the String parameter passed.

Coming to the test code:

Period period = Period.valueOf(<value>);

switch (period.getId() )

{

case Period.ID_ONE:

....

....

(1) Pass the String value to the valueOf() method to get the Period.

(2) Get the ID for the period retured and test it in the Switch case method.

Hope this helps.

Regards,

Rekha Malavathu

Former Member
0 Kudos

Thanks Ashu/Rekha..

former_member182372
Active Contributor
0 Kudos

Gandolf, this is classical implementation of enumerations in Java. Constructor is private so you can`t instantiate objects outside the class and can use only set of values wich is predefined in the class itself (and defined public and statically so you can use them without instantiating object) - only one value "ONE" in this case.

Former Member
0 Kudos

Thanks for the reply. I got what you are trying to mention.

There are a series of constants in my program. I was just taking an example of 'ONE'.

Can you explain me what this line mean ?


public final static Period ONE = new Period("ONE", ID_ONE);

I suppose this line to create a HashMap - periods.


private static HashMap periods = new HashMap(1, 1.0f);
static {
		periods.put(ONE.toString(), ONE);
	}

Finally this line I guess is to return one record from the HashMap period and get the ID of the selected record using getId():


Period period = Period.valueOf(<value>);
	switch (period.getId() 

Please correct me if I am wrong.

Regards

Gandolf