tags:

views:

209

answers:

10

in php you can declare a function in a global scope with function.

is this not possible in java? looks like every function is in a class as a method. so everything is OOP in java? no procedural code?

+17  A: 

The closest thing you can have to a "free-floating" function is a static function, which you can call as qualified with the class name, e.g.

public class Foo {
   public static void bar(){}
}

... elsewhere

Foo.bar();

You can add a bit of syntactic sugar to this to make it look like what you're thinking of:

import static Foo.bar;

... elsewhere

bar();
Steve B.
You can also import the function by itself:`import static packagename.Foo.bar;` But if you do this too much it can get confusing.
MatrixFrog
ahh, that's what I meant,thanks.
Steve B.
+3  A: 

Yep. But you can define static methods, which ultimately can act as methods contained within a class but be invoked without instantiating an instance of the class. That is, if you define a method bar as static in class Foo then it may be invoked as Foo.bar().

Mark E
+1  A: 

Yes, that is correct. Java is strictly OOP.

Alex JL
+3  A: 

That's right, no procedural code, everything's an object defined by a class (except the few primitive data types). You can, however, use static methods:

public class MyClass {
    public static void callMe() {
        System.out.println("HEY");
    }
}

public class MyOtherClass {
    public void someMethod() {
        MyClass.callMe();
    }
}

The JVM-based language Scala does allow you to create higher-order functions, which you can pass around as values.

Kaleb Brasee
Cool, so Scala gen's up the anon class garbage for you. I really need to sit down and *read* my bloody Scala book to see how much of the closure, "currying" and other function-ish stuff it rolls in.
Roboprog
I got the book and read through it, and it's quite a powerful language. However, I found the dozens of shortcut operators that represent these new features to be a bit confusing and hard to remember. Ruby seems to be easier to get started with (too bad it's dynamic and 50x slower than Java though, LOL).
Kaleb Brasee
I keep hearing that Java is big-N times faster than Ruby. That might be true for x += y kinds of math-y/primitive stuff, but it is NOT what I measured for some basic string jiggering: http://roboprogs.com/devel/2009.12.html (of course, these "benchmark" only uses a few static methods, the only instances thrashing around the GC of either language are built-in strings)
Roboprog
+3  A: 

Oh yes, OO indeed. You can code pseudo procedural stuff within a static method though.

o.k.w
+3  A: 

You can make a method static an call it without creating an Object

public class MyClass{
   public static void m(){
      ...
   }
}


MyClass.m();
Fabiano
+3  A: 

Expanding on what everyone else is saying, if you have a lot of such functions, it is common to group them together in classes that are full of nothing but static methods, often called "utility classes." The first example that comes to mind is java.lang.Math but there are tons of others. Any class with "Util" in its name is likely to follow this pattern.

MatrixFrog
A: 

Do not jive well well OOP. You can still have define a public static method and call it from anywhere as others have mentioned.

fastcodejava
A: 

In java world, all functions must be declared within a class, no matter what. Welcome to OO world =)

Daniel
A: 

Yes, you can make static functions, er, methods, but then you have to hard-code calls to them. To select a callback at runtime, you have to make an interface (not a function pointer, procedural type, or delegate), and then make a bogus class (though perhaps anonymous) to "implement" it. Enjoy the make-work!

Or, enjoy the humorous satirazation: http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html

Roboprog