0

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!

Midas
  • 23
  • 2
  • "I think it is mainly a syntax error?" The compiler is very good at telling you if there's a syntax error. What does the compiler say? – Kayaman Aug 11 '17 at 08:41
  • unknown class dbmessage(in 2nd line of main) – Midas Aug 11 '17 at 08:45
  • Include all of the main class in your question. – Kayaman Aug 11 '17 at 08:49
  • the code you posted is compilable.... can you be a little more specific about the meaning of ***"this doesnt work"*** – ΦXocę 웃 Пepeúpa ツ Aug 11 '17 at 08:53
  • Thank you all for taking the time and effort to answer so quickly to my question. Sorry for any confusion caused. The problem is that the compiler(android studio) comes back with the message that dbmessage is an unknown class. – Midas Aug 11 '17 at 08:56
  • Add the full error message to your question please @WimSteenbeek – Asew Aug 11 '17 at 08:57
  • 1
    Edit your question to include the full code of your main class as well as the full error message. – Kayaman Aug 11 '17 at 09:00
  • show us the full code and error that you are getting – Bharat Aug 11 '17 at 09:15
  • You're trying to run code outside of a method. You really need to go through the basics, you're flying absolutely blind here. – Kayaman Aug 11 '17 at 11:06
  • You guys are absolutely right! I was so focussed on the class, thinking that the mistake was in there that I placed the code outside a method! indeed, a stupid mistake, but thanks a lot for pointing this out. Cheers for the effort guys! – Midas Aug 11 '17 at 15:12

3 Answers3

0

If the code you gave us from class Main is complete (it can't be, because the imports are missing), you try to write statements outside of a method. That's not allowed in Java syntax. A correct snippet would be:

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);
    }
}

I'd recommend to get good tutorials / textbooks on both Java as well as Android programming. Otherwise it will be a frustrating experience. Surely there will be chapters on the Java style guide, among others.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7
  • you are 100% right! I was so focussed on the class, thinking that the mistake was in there that I placed the code outside a method! indeed, a stupid mistake, but thanks a lot for pointing this out! – Midas Aug 11 '17 at 15:13
-1

Check this post for array of class instanciation.

Although I would recommand you to use an ArrayList, which are kinda similar to vector in C++.

List<Message> myList = new ArrayList<Message>();
myList.add(new Message("TEST MESSAGE", Message.Type.warning,Message.Actions.no,1));
Asew
  • 374
  • 2
  • 13
  • It doesn't answer the question. It's still unclear *what* the actual question is. – Kayaman Aug 11 '17 at 08:53
  • The first sentence of the post is "I have question about how to make an array of classes". The link I provided answer that question already, and I just add a bit of personnal advice. – Asew Aug 11 '17 at 08:55
  • He knows how to create an array, the code he's displaying is syntactically correct. We're trying to find out what his **real** problem is. If he just managed to give useful information. – Kayaman Aug 11 '17 at 09:03
-1

Your problem is that you are refering a non-static variable in a static context. What you need to do is something along the lines of: Create an instance of the mainclass, Create a non-static void that does what you want Call that void from the class instance: This works for me:

class Program
{
    public static void main (String[] args)
    {
        Program p = new Program();
        p.Run();
    }

    public void Run()
    {
        Message[] array = new Message[300];
        array[0] = new Message("TEST MESSAGE", Message.Type.warning,Message.Actions.no,1);
    }
}
aPlutonicCoder
  • 116
  • 1
  • 11
  • Nothing to do with classes being static or non-static. – Kayaman Aug 11 '17 at 08:55
  • I meant the actual array. You can't access it because main is static – aPlutonicCoder Aug 11 '17 at 10:58
  • The array is a local variable in `main` as you can see. – Kayaman Aug 11 '17 at 11:05
  • I don't care about your code. You weren't the one asking the question. – Kayaman Aug 11 '17 at 11:06
  • He is getting an error message because he was refering a non static variable in a static context – aPlutonicCoder Aug 11 '17 at 11:07
  • I read the question and I posted code that works and solves his problem – aPlutonicCoder Aug 11 '17 at 11:11
  • Have you tried putting the code in your (badly named) `Run()` method into `main` instead? Because that compiles and runs just as well. It's not an issue about static vs. non-static. – Kayaman Aug 11 '17 at 11:15
  • Main.java:20: error: non-static variable this cannot be referenced from a static context array[0] = new Message(); – aPlutonicCoder Aug 11 '17 at 11:18
  • Congratulations. You don't know how to write code either. – Kayaman Aug 11 '17 at 11:19
  • Man.. just be useful and post your solution – aPlutonicCoder Aug 11 '17 at 11:20
  • There is no solution. The asker doesn't understand the basics, and he's trying randomly to get things to work. Giving bad answers doesn't help him, and there are no good answers to give. – Kayaman Aug 11 '17 at 11:23
  • Dear @Kayaman, You are very knowledgeable in Java(and I guess more coding etc). You also gave the correct answer amongst others. But why are you so assertive, edging on agressive? I stated that I am trying to learn, and just started Java. So why the interesting attitude? Best regards, – Midas Aug 11 '17 at 15:23
  • @Midas It's because programming turns you into a bitter old man, and you start to resent everything in this world. – Kayaman Aug 13 '17 at 20:12