0

I've asked this question Adding objects to an array. And now it has brought up another question.

Is there a difference between this: For class Patient:

public Patient(String ptNo, String ptName, int procType) throws IOException
{
    Patient.patientNo =  ptNo;
    Patient.patientName = ptName;
    Patient.procedureType = procType;
}

and this:

public Patient(String ptNo, String ptName, int procType) throws IOException
{
    this.patientNo =  ptNo;
    this.patientName = ptName;
    this.procedureType = procType;
}

I thought they were the same thing?


edit I have created these in the Patient class.

private static String patientNo;
private static String procedureDate;
private static String patientName;
Community
  • 1
  • 1
  • 1
    You really need to read about the difference between instance attributes and class (static) attributes. – Ingo Nov 07 '13 at 11:15
  • Then perhaps you should try to tell us how it does not make sense, where exactly you get stuck and don't understand. The answers below state the case quite clearly, IMHO. – Ingo Nov 07 '13 at 11:20
  • 1
    @Skippy-psI'mawoman It is perhaps too easy, amybe? You know that "the nose" doesn't mean any concrete nose, rather we talk about "my nose", "your nose", "Jimmies nose", etc. Now, `this.nose` is Javas way to say "the nose of the thing currently under consideration". Bottom line: every human has (hopefully) a nose, hence "nose" would be an instance attribute of the class Human. – Ingo Nov 07 '13 at 11:31

4 Answers4

4

this is used to the access the properties that belong to an object whereas Classname is used to access the properties that belong to a class. Properties belonging to a class/static attributes means that those will be shared by all the objects.

Suggested reading: Understanding Instance and Class Members

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
3

You use this to access to the non-static members of the class, whereas ClassName is used to access the static members. The non-static fields of a class are unique to each instance of the class, whereas the static fields are common to all instances.

class Test{

    String txt;
    static String sTxt;

    public Test(String t) {
        this.txt = t; // accessing the instance variable using this
    }
}

class Test2{

    void someMethod(){
        Test t = new Test("someString");
        Test.sTxt; // Accessing the static member, using classname
    }

}
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • @RomanVottner - Exactly, and that's why I didn't mention that, as it is a bad practice, and the OP shouldn't try it, even for testing purposes! :) – Rahul Nov 07 '13 at 11:20
3
public Patient(String ptNo, String ptName, int procType) throws IOException
{
  Patient.patientNo =  ptNo;
  Patient.patientName = ptName;
  Patient.procedureType = procType;
}

This code will only work if all three variables are static like this

public class Patient
{
 static String patientNo;
 static int procedureType;
 static String patientName;

 public Patient(String ptNo, String ptName, int procType) throws IOException
 {
     Patient.patientNo =  ptNo;
     Patient.patientName = ptName;
     Patient.procedureType = procType;
 }
} 

Whereas something like this

public Patient(String ptNo, String ptName, int procType) throws IOException
{
   this.patientNo =  ptNo;
   this.patientName = ptName;
   this.procedureType = procType;
}

will work for non static variables, "this" simply is used to avoid confusion denote the object whose constructor is being called. Example

Patient p = new Patient("A","B",1);

would be same as saying

   p.patientNo =  ptNo;
   p.patientName = ptName;
   p.procedureType = procType;

EDIT

"this" comes to rescue in a situation like this

public class Patient  
{
  String patientNo;
  int procedureType;
  String patientName;

  public Patient(String patientNo, String patientName, int procedureType) throws IOException
  {
     this.patientNo =  patientNo;      // variables in LHS are class members, RHS are method parameters
     this.patientName = patientName;
     this.procedureType = procedureType;
  }
} 
Ankit Rustagi
  • 5,539
  • 12
  • 39
  • 70
2

this is used to access Object properties. It is typically used to access non-static variables of the class, as they are associated with the object.

className is typically used to access static variables, as static variables are associated with the class.

Static variable are initialized when class loads where as object variable are initialized only when object of class is created.

Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72