16

I've recently (4 days ago) started programming in JAVA. I have some overall programming experience from C++ and PHP. My question is: can we implement a function in JAVA, that is available in all classes? I'm thinking of some global logging function, that I need to call in several places (log events, errors, etc.).

Imagine I have two classes, A and B. I need to call logging function in both of them, but I don't want to copy whole function body (awful thing I believe), and I want to call it strict (without creating another class, instantiating it, and then calling from the instance), like logEvent(someVariable). So I should use an abstract class C, which A and B will extend, BUT they are already an extension of other class (built-in). Since multiple inheritance isn't allowed (is it?), I need to do some trick. Singleton is not pleasing me too. In PHP or C++ I would just create separate file with function body and then include it.

Here is how I want to use it:

public class A extends SomeClass {
    String error = "Error from class A";
    logEvent(error);
}

public class B extends SomeOtherClass {
    String error = "Error from class B";
    logEvent(error);
}
Mars
  • 867
  • 2
  • 13
  • 22

3 Answers3

37

Put a static method in any class (it could be a utils class, or whatever), then call it like this: ClassName.functionName()

Static methods belong to the class, not instances of the class, so you don't need to instantiate the class to access the method

But everything in Java has to be in a class, so you can't access it without the class name.

Vyassa Baratham
  • 1,457
  • 12
  • 18
  • 1
    You can also have a static method that returns the single allowed instance of the class if you don't like static methods. Make the class constructor private and keep the one instance in a static field. – Lee Meador Jul 29 '13 at 21:33
  • 4
    You can also use a static import so that you can use the static method without qualifying it with the class name at each use site. – DaoWen Jul 29 '13 at 21:35
  • @DaoWen good point, I forgot about that feature of Java. For more info: http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html But keep in mind static imports are somewhat frowned upon, they can be easily misused. I don't think having to type the class name every time is that much of a big deal, and it keeps the code clean. – Vyassa Baratham Jul 29 '13 at 21:40
  • Ok. That's acceptable, but now I need to make extra import :D. But from my point of view it's better that way, than writing preceeding with `Class.` everytime. Hoped there is better solution, but after long research I couldn't find it. Thanks for replies :) – Mars Jul 29 '13 at 22:08
  • Is there any pros and cons of this method? – NOT_A_PROGRAMMER May 05 '16 at 15:25
  • To understand why this is the case, you need to understand Java Class Loading mechanism. Everything running in a JVM has to be loaded from some Class file. Loading starts when you point JVM at some class (with main), and the rest of the code gets loaded transitively via imports. – igorlord Jun 03 '18 at 20:18
17

You should use static method:

package xxx;

public class Util{
    public static void logEvent(String error){
    ...
    }
}

and import static:

import static xxx.Util.*;

public class A extends SomeClass {
    String error = "Error from class A";
    logEvent(error);
}
user2952801
  • 171
  • 1
  • 2
2

You may use static method.

Define a class with a static method:

public class Util{
    public static void logEvent(String error){
        ...
    }
}

Then, you can use static metod like this way:

public class A extends SomeClass {
    String error = "Error from class A";
    Util.logEvent(error);
}

you may take a look here to learn more about static method, http://www.leepoint.net/notes-java/flow/methods/50static-methods.html

Jacky
  • 58
  • 7
  • I know about static methods, but I would like to avoid writing class name everytime i use them (after 20 or 50 calls it could be frustrating :) ). But I can do static import as @DaoWen stated above. – Mars Jul 29 '13 at 22:01