-JAVA-Im trying to dynamically create objects and then slap them on array list

Pages: 123
Satire? I couldn't imagine programming in a language where the code didn't work if you didn't use the proper combination of case in your identifiers.
kay maybe i shoulda said 'how do i dynamicaly call object methods(?)' from a list thing
What do you mean, "dynamically call object methods" "from a list"? Can you provide an example scenario or some pseudo code?
Last edited on
oh sry here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 public void getobjrecipie(String compare)
    {
        int num = 0;
         try {
             for (recipie recipes: objlist)
              {
                  num++;
               if (recipes.getname().equals(compare))
               {

                   break;
               }

              }
             }
         catch (Exception e)
         {
          System.out.printf("nah we don't have that, or ... %s", e);
         }
        objlist.get(num).printrecipie();//this is wrong, it works but i think its not correct, that and catch is called :/
    }
}
anyone?
1
2
3
4
5
6
7
8
9
10
11
public Recipe getObjectRecipie(String name)
{
    for(Recipe r : recipe_list)
    {
        if(name.equals(r.name()))
        {
            return r;
        }
    }
    return null;
}
1
2
Recipe recipe = getObjectRecipe("Pickaxe");
recipe.doStuff();
Last edited on
Hur-Javas weird.
You would do the same in C++, except with pointers.
How can i learn to know how to do stuff like that?


I love coding but the easy stuff sure bends my head in new ways, i should be much better by now really
Last edited on
@LB do you know why i get an error "non static method cannot be referenced from static context?" I solved this before by having said thing in a function, this time its i a function :/ and if i take off extends it says cannot resolve, i guess its inherited nothing, oh dear bended head its late i need sleep and WAY more practice.

Im trying to convert the problem into c++ness so i can solve it in mah head
For what line of code does it say that? It sounds like you're trying to call an instance method (a method without the static qualifier, hence a non-static method) via the name of the class (which is a static context) instead of via the name of an object or 'this' (which is an instance/non-static context)
but its been made dynamically it doesnt have a name :(
Last edited on
Change "public Recipe getObjectRecipe" to "public static Recipe getObjectRecipe"
Last edited on
if i do that then the array list cant be used
This is why we dont ask java problems in a c++ forum, people arnt as good at it :P
devonrevenge wrote:
This is why we dont ask java problems in a c++ forum, people arnt as good at it :P


Eh! Look at post # 2 in this thread. There're already Java classes for what you're trying to accomplish -- no need to make your own (based on your stated goal).

* Don't take it personally, but the standard implementations (minus certain hack-jobs therein) are generally better to use than hand rolled randoms.
Last edited on
@devonrevenge the main method in Java is Static, so everything you want to call from it also has to be Static. Static is an infectious disease, remember?
@LB

Static has its (very good) uses. Responsibility is the key.
@LB

You don't need to use any static besides one in main.

@Luc Lieber

Static does not belong to the OOP world. While it may have some good use cases - to implement singletons reliably or static constants or static (stateless) utilities methods, it is like goto in structural programming. The better your language is at OOP, the less you need static (Scala dropped static from Java in favor of native support for singletons, Javascript also doesn't have it - programmers are very happy with lack of static).

In most cases appearance of static state in your code means designers were lazy and didn't do the architecture well enough.
Last edited on
@Luc Lieber, I havn't learnt hash and map stuff yet, guess it was already time, You know i looked at that and thought i would learn it later, Bucky didnt let me in on that secret did he, nooh, he kept it to himself >:(


Can you help me finish this hackish hackjob tho, or is it impossible?


yeah theres no way i could use the objectlist if eveything starts being static because theres an error.

Im not sure if i have perfectly grasped static now :/
Last edited on
Pages: 123