0

I start with OOP And i have following problem: I made a new class Then I made ainstance from this class Now, for every instance I want to do something I tried it with a for each loop but it doesn't work... There are some syntax problems

This is the class:

package main;

public class command
{
    String call;
    String execute;
}

And this from the Main class:

 private static void load() {
        command greeting = new command();

        greeting.call = "hello";
        greeting.execute = "Hello Sir";


        for (command c: command) {
            System.out.println("Another command...");
        }

    }

I don't know how to make the loop or is there another way to do it?

5
  • 1
    In order to use the for each, you need some kind of collection. Create, for instance, a List of commands, add 1 or more elements to this list and loop over that list. Commented May 29, 2019 at 13:43
  • To perform a iteration, you should put instances in array or Iterable container, e. g. List Commented May 29, 2019 at 13:43
  • 1
  • @Robert Kock I know but for this i must do this for every instance and that's too long because later I will create the instances from a xml file and then i've hundreds of instances. To do this automaticly i must go through the instances with a loop. And that's my problem Commented May 29, 2019 at 13:50
  • @TristanK. see the answer of Bastien. It's quite clear Commented May 29, 2019 at 13:53

2 Answers 2

1

You can create a static list inside class command that the instances get added to in the constructor(s). Then you'll always have references to whatever instances are created.

Here's an example:

import java.util.List;
import java.util.ArrayList;
public class command
{

    String call;
    String execute;

    public static List<command> commands = new ArrayList<>();

    public command() {
        commands.add(this);
    }

    public command(String call, String execute)
    {
        this.call = call;
        this.execute = execute;
        commands.add(this);
    }

    public String toString() 
    { 
        return "call: " + call + " | execute: " + execute;
    } 

}

Driver class:

public class driver
{
    public static void main(String[] args)
    {
        for(int i = 1; i <=10; i++)
        {
            command c = new command("call" + i, "execute" + i);
        }

        for(command cmd: command.commands)
        {
            System.out.println(cmd);
        }
    }
}   

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

The syntax you are using in your for loop must use a instance of a class that implements the Iterable interface. For example you can use implementations of the List interface.

For example, you can try:

private static void load() {
    command greeting = new command();

    greeting.call = "hello";
    greeting.execute = "Hello Sir";

    List<command> listOfCommands = new ArrayList<>();
    listOfCommands.add(greeting);

    for (command c: listOfCommands) {
        System.out.println("Another command...");
    }

}

2 Comments

I know but for this i must do this for every instance and that's too long because later I will create the instances from a xml file and then i've hundreds of instances. To do this automaticly i must go through the instances with a loop. And that's my problem Ok later when i read them from a xml file i will put them automaticly in a array Thank you
Not sure to understand clearly what you want to do. If you want to store every instance of a class, so that you can easily access them later, you can use a static list, on which you would append every instance of the class when it's created, but I'm not sure it's what you're looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.