Question:
java ..do i have to provide default value with constructor even after initialize them?
John
2011-07-13 22:40:46 UTC
as you see i have 2 classes and my question is what if i dont provide default values to instance variables with using constructor?because i already initialize them (private String name="" etc)?
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");

}

}
Three answers:
modulo_function
2011-07-13 22:51:28 UTC
Java allows you to intialize the variables like you've done. Then when you construct an object the fields have those values.



Does that answer your quetion? One thing you could do to verify it for yourself is construct and object and then print it's fields and you'll see that that object has the intialial values that you've set.
deonejuan
2011-07-13 23:39:25 UTC
Change your member variable

private String name="";

to

private String name;



and your constructor will fail without an argument given at invocation

SomeTester noArg = new SomeTester(); // would fail



add a blank (default) constructor to SomeTester

public class SomeTester {

private String name;

public SomeTester() {};

public SomeTester( String name ) {

this.name = name;

}



now, it will work

SomeTester noArg = new SomeTester();

but

noArg.name; will fail because the variable is not assigned, not initialized.
spero
2016-12-18 18:27:21 UTC
The "this" key phrases ability that this is relating techniques and variables from this way. case in point, interior the constructor "public Rectangle(int x, int y, int width, int height) ", you have the line "this.x = x;" which ability the internal maximum int x of this way is being set to the int x that grew to become into exceeded as a parameter in this constructor. although, the constuctor: public Rectangle(int width, int height) { this(0, 0, width, height); } makes use of this because of the fact while the constructor with basically the width and height is being given, it makes use of the backside constructor yet plugs in 0 and nil for x and y. observe how basically 2 values have been given (width and height) yet once you're calling "this(0, 0, width, height);" there are 4 values? this means that this is calling the constructor of this way that takes 4 integers. comparable for the empty constructor.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...