John
2011-07-13 22:40:46 UTC
giving to the variables default values with constructor and initialize when i declare them.this 2 things are same thing?can you please explain what if i dont use the constructor to provide default values?but initialize when i declare them?
import java.util.*;
public class SomeTest
{
// i already initialized them why i need to use the constructor to provide default values?
private String name="";
private int score=0;
private int idNumber=0;
public SomeTest(String name) // my constructor
{
this.name=name;
}
public void setTest()
{
String name="";
Scanner kb=new Scanner(System.in);
System.out.print("Your Name is : ");
name=kb.nextLine();
this.name=name;
}
/* or i can use this public void setTest(String name)
{
this.name=name;
}
*/
}
public class SomeTester {
public static void main(String[] args)
{
SomeTest sr=new SomeTest("something");
sr.setTest();
//or sr.setTest("bla bla");
}
}