Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By jim829

Thread: creating an array of functions

  1. #1
    plot is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default creating an array of functions

    I did this:

    Java Code:
    practice[] f = new practice[50];
    	
    f[0] = new function0();
    I get an error saying: Syntax error on token ";", { expected after this token

    Also the function function0() takes a boolean if that adds any strange complicaitons, I looked online and this was the correct way to create an array of functions but I get an error and not sure why and was hoping if anyone can tell me what I am doing wrong.

    thank you

    edit:

    Also I need help to calling those functions in the array of functions


    edit2:
    Here is a simple code I want to run using array of funcitons
    Java Code:
    public class Practice 
    {	
    	Practice[] f = new Practice[2];
    	
    	f[0] = new function1();
    	f[1] = new function2();
    	
    	public static void main(String args[])
    	{
    		boolean b = false;
    		
    		b = f[0]; //need to call the function here
    		if(b == true)
    			System.out.println("Hello World");
    		b = f[1]; // need to cal function
    		if(b == false)
    			System.out.println("Bye World\n\n");
    	}
    	
    	boolean function1(boolean b)
    	{
    		b = true;
    		return b;
    	}
    	
    	boolean function2(boolean b)
    	{
    		return false;
    	}
    }
    Last edited by plot; 01-02-2014 at 10:16 AM.

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: creating an array of functions

    If function0() is truly a function then why on earth are you trying to use the new keyword to call it? I would assume it is actually a very improperly named class and you mean to say "constructor" where you are now saying "function".

    And if it takes a boolean then why are you not passing one to it?

    I am wondering if this is even a question about Java at all or perhaps another language.
    Last edited by gimbal2; 01-02-2014 at 09:39 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    plot is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: creating an array of functions

    idk I saw it online on a couple websites so I thought to copy that but even if I take out that new keyword and pass a boolean I still get the same error.

    If you take out the new keyword I think it will instead call that function and return a 4d array (which is the return type of my function) rather adding a function into my array.

    I want to create an array of functions which i can call latter in my program which can help me to avoid hard coding all my function calls and I can just go through a for loop to call all my functions. I hope what I said makes sense...

  4. #4
    plot is offline Member
    Join Date
    Dec 2013
    Posts
    12
    Rep Power
    0

    Default Re: creating an array of functions

    well I know in C you can create an array of functions that can be passed though other functions or and be called, pretty sure java should have that capability...

  5. #5
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: creating an array of functions

    Java Code:
    Boolean[] bool = new Boolean[2];
    bool[0] = function1();
    bool[1] = function2();
    I sometimes forget that java primitive types also have Objects.
    Last edited by zFollette; 01-02-2014 at 10:49 AM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,936
    Rep Power
    4

    Default Re: creating an array of functions

    That is not creating an array of functions. That is simply calling a method and assigning the return value to the array element. What I believe the OP wants to do cannot be done in Java. In C an array of functions is referred to as function table and employs function pointers. The closest thing in Java is employing a strategy design where you create a class with no state and just a single method. Then pass an instance of that class to another class or assign it to an array element. But in the case of Java, each array element method name is the same, only the implementations vary. Ideally, one would use an interface for this.

    Regards,
    Jim
    Last edited by jim829; 01-02-2014 at 04:11 PM.
    gimbal2 likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: creating an array of functions

    Quote Originally Posted by plot View Post
    well I know in C you can create an array of functions that can be passed though other functions or and be called, pretty sure java should have that capability...
    It doesn't.

    You can create an array of Methods, but reflection is slow and you will not be able to refactor the code easily. See Method (Java Platform SE 7 ).

    A better option would be to create an interface with one method that has the signature you need, then create an array of references to that type. If a void method with no parameters will do, you could just use a Runnable[]. I forget what this design pattern is called, but it's pretty common in Java.
    Get in the habit of using standard Java naming conventions!

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: creating an array of functions

    The closest thing to what you're talking about is lambda expressions, and Java will support them starting with Java 8: Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2012, 01:50 PM
  2. Creating Array
    By gnawk in forum New To Java
    Replies: 3
    Last Post: 03-06-2012, 02:04 AM
  3. Creating Array of Objects
    By chathurajeewaka in forum New To Java
    Replies: 4
    Last Post: 12-03-2009, 03:23 PM
  4. Creating a Defined Class with Member Functions
    By New2Java in forum New To Java
    Replies: 6
    Last Post: 08-05-2009, 09:05 PM
  5. creating array at runtime
    By javaplus in forum New To Java
    Replies: 4
    Last Post: 11-08-2007, 10:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •