Reputation: 91
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;
}
}
Upvotes: 0
Views: 104
Reputation: 3407
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
}
Upvotes: 0
Reputation: 106430
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.
}
Upvotes: 1
Reputation: 46375
Date and descrip need to be defined at the class level not as local variables.
Upvotes: 0
Reputation: 8942
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;
// ...
}
Upvotes: 0