I have a question about how to make an array of classes. My background is in embedded C, so Java is a bit new for me.
In C you could make a struct. In java I found that this is not possible, so I created a class:
public class Message
{
public String Text;
public enum Type {warning,caution,advisory,engineering};
public enum Actions {yes,no};
public int priority;
public Message(String Text,Type nType,Actions nActions,int priority)
{
this.Text = Text;
type = nType;
actions = nActions;
this.priority = priority;
}
private Type type;
private Actions actions;
public String getText() { return Text; }
public void setText(String Text) { this.Text = Text; }
}
So I kind of used this as a "struct" (in C).
I would like to make an array of this class.
What I did in the main:
public class Main extends AppCompatActivity
{
SurfaceView cameraView;
TextView textView;
CameraSource cameraSource;
final int RequestCameraPermissionID = 1001;
Message[] dbmessage;
dbmessage = new Message[300];
dbmessage[0] = new Message("TEST MESSAGE", Message.Type.warning,Message.Actions.no,1);
}
WHAT I SHOULD HAVE DONE IN THE MAIN: (BEST ANSWER)
public class Main extends AppCompatActivity
{
SurfaceView cameraView;
TextView textView;
CameraSource cameraSource;
final int RequestCameraPermissionID = 1001;
// you have to put it into a method.
public void myMethod() {
Message[] dbmessage;
dbmessage = new Message[300];
dbmessage[0] = new Message("TEST MESSAGE", Message.Type.warning,Message.Actions.no,1);
}
}
The compiler shows an error: "unknown class dbmessage" I think it is mainly a syntax error?
Would any of you be able to shine a light on this, or give me a kick in the good direction?
Please be patient with me, as Java(or even programming) is not my profession and I am just trying to learn.
EDIT: Thanks to multiple members pointing out that I used code outside a method. This is the solution(obviously). The code from the MAIN is now placed in a method and works perfect. Thank you guys for the patience and the help!