-2

I'm not sure what I'm doing wrong, but when I build, I get the error:

Cannot find symbol

for both of these statements. Here is the constructor.

public Course
{ 
    public Course(String cName,String cNum,ArrayList<Module> mod)
    {                         
       this.cName = cName;
       courseNum = cNum;
       this.mod = mod;                     
    }
}

Is this what I have to do? If so, why not just forgo the "this" keyword and just use cName = cName? What purpose does the "this" have?

public course
{
    String cName;
    public Course(String cName)
    {
        this.cName = cName;
    }
}   
Pang
  • 9,564
  • 146
  • 81
  • 122
Decon21
  • 55
  • 5
  • 5
    Well first of all, `public Course { }` is not proper syntax. Should be `public class Course { }`. I highly recommend viewing some Java tutorials online – Vince Nov 19 '15 at 02:27
  • 2
    and the constructor name has to match the class name, including how it is capitalized. – Thilo Nov 19 '15 at 02:28
  • 2
    Considering you are super new to java, I would suggest reading books or tutorials rather than asking random questions. Because there will be lots and lots of unnecessary discussions. Something like - http://www.onlineprogrammingbooks.com/object-oriented-programming-using-java/ – brainless coder Nov 19 '15 at 02:30
  • Thanks to everyone for the answers! I'm sorry this was a duplicate. I had searched quite a bit but found nothing that quite answered my questions. Also, I'm very sorry for any mistakes I may have made, I'm new to all of this and am working through the tours still! Thanks very much to everyone for your very prompt help!! – Decon21 Nov 20 '15 at 02:54

5 Answers5

2

Your first example does not work because the fields you are trying to assign, cName, courseNum, and mod, are not declared in the class. You need to declare the fields in order for the assignments to work. Make the variables private to make sure that they are well encapsulated:

public Course {
    private String cName;
    public Course(String cName) {
        this.cName = cName;
    }
}

The reason you need to use this is to disambiguate between cName-the-parameter and cName-the-instance-variable. You could disambiguate in some other way - for example, by renaming one of the variables:

public Course {
    private String cName;
    public Course(String name) {
        cName = name; // Using "this" is no longer required,
                      // because names are different.
    }
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

It is an issue of having two variables of the same name in the same scope. The most local one shadows/hides the other one, and this. is needed to reach out to the hidden variable.

why not just forgo the "this" keyword and just use cName = cName?

That wouldn't do anything. Just assign cName to itself. Helpful compiler may give you a warning.

Whereas what you really want is to assign the local variable (method argument) to the instance variable (field). Since the two unfortunately have the same name (outside of a constructor that would be considered a style issue), you have to disambiguate by saying this.cName if you want to instance variable.

Note that it works without this for your second variable, where the parameter and field have different names:

courseNum = cNum; // no this
Thilo
  • 257,207
  • 101
  • 511
  • 656
-1

The variables prefixed by this. will reference the instance variables for the class. this.cName = cName will assign the value for the cName variable declared in the constructor's argument list to the cName class member variable.

If you just write cName = cName, it won't do anything because you're assigning the value of the variable declared in your constructor to itself.

Lawrence McAlpin
  • 2,745
  • 20
  • 24
-1

Your second code example is the correct way to format your code. To answer your question, the this keyword helps to clarify scope (only accesses an object's instance fields).

Using

public course
{
String cName;
    public Course(String cName)
   {
    cName = cName;
    }
}   

only assigns the parameter value back to itself, while using

public course
{
String cName;
public Course(String cName)
{
    this.cName = cName;
}
}   

assigns the value cName from the constructor parameter to the cName declared as a class level field.

deepmindz
  • 598
  • 1
  • 6
  • 14
-2

This should work,

public clcass Course
{ 
   String cName,courseNum; // instance variables
   ArrayList<Module> mod;  //// instance variable
   public Course(String cName,String cNum,ArrayList<Module> mod)
   {                         
      this.cName = cName;
      courseNum = cNum;
      this.mod = mod;                     
   }
}

Whenever you have the instance variable name same as the parameter name in the constructor ,you need a way to distinguish between the two. this keyword helps in this case. Eg - When you say this.cName in your constructor it refers to the instance variable cName and when you just say cName in your constructor it refers to the parameter name of your constructor.

Parag Kadam
  • 3,620
  • 5
  • 25
  • 51