简体   繁体   中英

“Cannot Find Symbol” in Sub Class Java

I am trying to make a sub class named " public class Once " and am getting the error " Cannot Find Symbol " on the lines " return date ;" and " return descript ;". I know its probably something really stupid I am missing, but any help would be great.

Here is my code!

import java.util.*;

public class Once
{
public Once(String dateIn, String descripIn)
{
  String date = dateIn;
  String descrip = descripIn;
}

public String getDate()
{
  return date;
}

public String getDescrip()
{
  return descrip;
}

}

You do not have those set as fields . A field defines a specific attribute about an object.

What you'll want to do is set them up as such:

public class Once {

    private String date;
    private String descrip;

    //initialize in constructor
    public Once(String dateIn, String descripIn) {
        date = dateIn;
        descrip = descripIn;
    }
    //Add getters and setters.
 }

You have defined date and descrip local in constructor.

It should be

public class Once{
    String date;
    String descrip;

    public Once(String dateIn, String descripIn)
    {
      date = dateIn;
      descrip = descripIn;
    }
    // other methods
}

Those variables are only in the scope of the Once() method. You need to declare them inside the class scope:

public class Once
{
    String date, descrip;
    // ...
}

日期和说明需要在类级别定义,而不是作为局部变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM