Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java function (method) available everywhere (global)

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);
}
like image 993
Mars Avatar asked Jul 29 '13 21:07

Mars


People also ask

Does Java have global functions?

Global variables are not technically allowed in Java. A global variable is one declared at the start of the code and is accessible to all parts of the program. Since Java is object-oriented, everything is part of a class. ... A static variable can be declared, which can be available to all instances of a class.

Can a method exist outside a class in Java?

In Java, there are only classes; nothing exists outside a class. Edit You can have public methods in non-public classes, but you probably don't want that since the non-public classes will have limited (perhaps none) visibility outside of the declaring class.

What is global method in Java?

A Java global variable is a variable that is declared outside a method or at the start of a code is a global variable, there are no access restrictions on a global variable. A Java global variable is mostly declared in the first line of code so that it can be used afterward.

What is method function in Java?

Method in Java or Java Method is a collection of statements that perform some specific task and return the result to the caller. A Java method can perform some specific task without returning anything. Methods in Java allow us to reuse the code without retyping the code.


1 Answers

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.

like image 114
Vyassa Baratham Avatar answered Sep 18 '22 03:09

Vyassa Baratham