Question

I am asking this in response to this answer.

I'm using an example of Variable and Var- edit Pls note I am asking where to use Var or Variable:

Class NewClass {

    private  String Variable = "";

    Public Class (String Var)
    {
        NewClass.Var = Variable;
    }
}

OR

    private  String Variable = "";

    Public Class (String Variable)
    {
        NewClass.Variable = Var; // OR WHATEVER OTHER COMBINATIONS IT MAY BE.
    }
}

Which ones are the class variables, how does this differ from the parameters and which goes where?

edit

I should add, as this was in the linked answer, but it seems people haven't looked at it:

It's particularly confusing because the parameters to the function have exactly the same names as the class variables, but Patient.ptNo is not the same variable as the parameter ptNo. (Actually, Patient.ptNo should be this.ptNo, because it belongs to this particular instance of the class. Patient.ptNo would refer to a single value that's common to all objects of type Patient.)

So when people say this.Variable = Variable I am still confused about what is what.

Was it helpful?

Solution 2

The class variable is the one defined by the class as a static field, and the parameter is the one defined by the method. It's just that simple. There are also instance variables (defined at the class level but not static) and local variables (defined within a method, but not as input parameters).

public class Foo {
    private static String someName; // this is a class variable
    private String someOtherName; // this is an instance variable

    public Foo(String anotherName) { // anotherName is a constructor parameter
        int yetAnother = 1; // yetAnother is a local variable
        someOtherName = "foo"; // assign a value to someOtherName
    }

These two variables are completely distinct. They don't even have to have the same type! The only complication in your example is that both variables happen to have the same name. When that happens, the compiler will favor the constructor parameter (or method parameter, or local variable) over the class variable. In order to "force" it to use the class variable, you prefix it with this..

The thing to keep in mind is that the two variables are totally separate, regardless of their names.

So this:

class NewClass {

    private  String Variable = "";

    Public NewClass (String Variable)
    {
        NewClass.Variable = Variable;
    }
}

is exactly the same as this:

class NewClass {

    private  String Variable = "";

    Public NewClass (String someOtherVariableName)
    {
        NewClass.Variable = someOtherVariableName;
    }
}

... and it's also exactly the same as this:

class NewClass {

    private  String Variable = "";

    Public NewClass (String Var)
    {
        NewClass.Variable = Var;
    }
}

The convention of using the same name for a parameter as for the class variable just means you don't have to come up with pointless variants on the variable name.

OTHER TIPS

No problem of using variable as your constructor parameter as it is the local variable for your constructor.

class NewClass {

    private  String Variable = "";

    Public NewClass (String Variable)
    {
        this.Variable = Variable;
    }
}

                       or

class NewClass {

    private  String Variable = "";

    Public NewClass (String Var)
    {
        this.Variable = Var;
    }
}

both are fine.

this.Variable denotes the current objects variable.

Try reading naming conventions.

this.Variable would refer to the Variable field of the class and just Variable would be the parameter passed to the constructor.

private String Variable = ""; - This is not static

Therefore your constructor would look liek

public NewClass(String Var/iable) {
    this.Variable = Variable; // this must be used, where this.Variable is the class variable and just Variable is the constructor parameter.
}

Note: You have used the ClassName.fieldName which is allowed only for static fields of the class. It should be this.fieldName instead, if the Variable is not static. Also, Public shouldn't be in caps, the constructor name should be the Class name. The keyword class should be in small and not Class.

In case you want to use the way you've put in your code, the Variable in the class should be static. Something like this

private static String Variable = "";
public NewClass(String Var/iable) {
    NewClass.Variable = Variable; // Since Variable is static now, you use the classname to access the static field.
}

Edit: It is absolutely okay to use the same name, as long as you can differentiate them both. In this case, this.Variable would mean the class variable and just Variable would refer to the parameter which was passed to constructor while creating an instance of this class.

class Variable = Variable passed to the constructor; // This is what the below statement means
this.Variable = Variable;
class NewClass {

private  String variable = "";

public NewClass (String variable)
{
    this.variable = variable;
}
}

this.variable refers to current object instance. Please follow naming conventions

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top