The class which contains main is the entry point in Java public class EntryPointClass { public static void main(String[] argv) { // This is where java programs start } } You can add methods to that same class public class EntryPointClass { public void welcomeMessage() { System.out.println("Welcome!"); } public static void main(String[] argv) { welcomeMessage(); } } in the above example we call the welcomeMessage() method inside the main method this will output Welcome! You can also make methods in other classes callable if they are public static and in an accessible package public class SomeOtherClass { public static void staticWelcomeMessage() { System.out.println("Welcome!"); } } Then in the main method class you would have public class EntryPointClass { public static void main(String[] argv) { SomeOtherClass.staticWelcomeMessage(); } } You could make the other class an object in which case you need a constructor public class SomeOtherClass { private String message; public SomeOtherClass(String msg) { this.message = msg; } public String getMessage() { return this.message; } } then in the main method class you would: public class EntryPointClass { public static void main(String[] argv) { SomeOtherClass test = new SomeOtherClass("Welcome!"); System.out.println(test.getMessage()); } } Answer from dionthorn on reddit.com
🌐
GeeksforGeeks
geeksforgeeks.org › java › function-interface-in-java
Function Interface in Java - GeeksforGeeks
July 11, 2025 - ... // Handling Multiple Conditions ... public class Geeks { public static void main(String args[]) { Function<Integer, Integer> addFive = a -> a + 5; Function<Integer, Integer> multiplyByTwo = a -> a * 2; // Applying functions ...
🌐
Mkyong
mkyong.com › home › java8 › java 8 function examples
Java 8 Function Examples - Mkyong.com
February 27, 2020 - Java8Function1.java · package ... public class JavaMoney { public static void main(String[] args) { Function<String, Integer> func = x -> x.length(); Integer apply = func.apply("mkyong"); // 6 System.out.println(apply); } } Output ...
Discussions

Functions In Java?
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
🌐 r/javahelp
5
3
January 10, 2022
Benefit of using Java Function rather than normal method? - Stack Overflow
There is no reason to use a function ... as your examples. Stack Exchange Broke The Law – Stack Exchange Broke The Law · 2021-08-12 09:33:02 +00:00 Commented Aug 12, 2021 at 9:33 · And in the reality, in java, these maldas assigned to functional interfaces are implemented as objects via anonymous classes... More on stackoverflow.com
🌐 stackoverflow.com
Methods vs Functions in Java
Java does not have functions, only methods. Functions are stand alone and exist outside the context of classes. Since this is not possible in Java, it doesn't have functions. Methods exist only in the context of classes. Even methods that one might consider functions abs, sin, etc. only exist in the context of the Math class and therefore are not functions. In Python, you can have both. If you just define a function outside any class, it is a function, yet if you define a function inside a class, it becomes a method. More on reddit.com
🌐 r/javahelp
36
5
August 7, 2023
Java: When to use method and when to create a class
In Java, you can have your main function inside a class, but in order to make it worthwhile, you have to still instantiate an object of that class. That can get kind of confusing, so I prefer to just have an App class with main(), and ALL it does is instantiate a single object. That's it, that object then handles everything else. For example, if I'm making a game, I might do this: public class App { public static void main(String[] args) { Game myGame = new Game(); Game.start(); } } The reason I like this is because it allows some separation of the main entry point from the actual logic of the program. To answer the other part of your question, the difference between a method and a class is this: A class is a structure - a collection of 'like' things. These things, together, make up an object. For example, a Car class might have wheels, doors, windows and so on. A method is an OPERATION on the class - i.e. something that can access information, change information, or requests the information in some way, shape or form. An example of a method on the Car class could be boolean openTrunk(). This method would check the state of the trunk, and if it's closed, open it and return true, else return false. So to 'prevent users from messing up my program', no, that doesn't have anything to do with it, whenever you make any kind of interactive input, such as a textbox, it is up to you to ensure the user doesn't do anything wrong. The best way to think about it: All users are idiots, and will try to enter their birthday where it says Address. You need to be prepared for that, and ensure it doesn't crash your program. Perhaps you might have a Validator class, with a method validateAddress(String input)? This is an example of how you could use both a Class and Method to prevent your user from messing things up. Hope I helped. More on reddit.com
🌐 r/learnprogramming
16
7
December 5, 2017
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › lambdaexpressions.html
Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
However, unlike local and anonymous classes, lambda expressions do not have any shadowing issues (see Shadowing for more information). Lambda expressions are lexically scoped. This means that they do not inherit any names from a supertype or introduce a new level of scoping. Declarations in a lambda expression are interpreted just as they are in the enclosing environment. The following example, LambdaScopeTest, demonstrates this: import java.util.function.Consumer; public class LambdaScopeTest { public int x = 0; class FirstLevel { public int x = 1; void methodInFirstLevel(int x) { int z = 2;
🌐
Reddit
reddit.com › r/javahelp › functions in java?
r/javahelp on Reddit: Functions In Java?
January 10, 2022 -

Hi,

I was primarily taught C++ in school so I honestly don't have a good clue as to how Java works. I'm basically wondering how I could use a function in Java. Let's say I write something called int getLargestInt(int n, int arr[]){}, which pulls the largest integer out of an array.

In c++, I would write this in another file, and then I'd include file into the main.cpp to be able to call this function. How would I do something like that in Java?

I've been reading some Cracking the Coding Interview/Programming Interviews Exposed, and they basically just have all of the functions into one huge file, but if I actually try to compile what they have it doesn't run.

Thanks for reading/helping :D

Top answer
1 of 4
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
2 of 4
1
The class which contains main is the entry point in Java public class EntryPointClass { public static void main(String[] argv) { // This is where java programs start } } You can add methods to that same class public class EntryPointClass { public void welcomeMessage() { System.out.println("Welcome!"); } public static void main(String[] argv) { welcomeMessage(); } } in the above example we call the welcomeMessage() method inside the main method this will output Welcome! You can also make methods in other classes callable if they are public static and in an accessible package public class SomeOtherClass { public static void staticWelcomeMessage() { System.out.println("Welcome!"); } } Then in the main method class you would have public class EntryPointClass { public static void main(String[] argv) { SomeOtherClass.staticWelcomeMessage(); } } You could make the other class an object in which case you need a constructor public class SomeOtherClass { private String message; public SomeOtherClass(String msg) { this.message = msg; } public String getMessage() { return this.message; } } then in the main method class you would: public class EntryPointClass { public static void main(String[] argv) { SomeOtherClass test = new SomeOtherClass("Welcome!"); System.out.println(test.getMessage()); } }
🌐
Tutorialspoint
tutorialspoint.com › java › java_functional_interfaces.htm
Java - Functional Interfaces
With lambda expression, we can avoid the need for both concrete as well as anonymous class objects. A functional interface assists one step ahead, as a lambda expression can implement the functional interface easily as only one method is to be implemented. Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method compareTo() is used for comparison purposes. But it can have any number of default and static methods. Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions.
🌐
Jenkov
jenkov.com › tutorials › java-functional-programming › index.html
Java Functional Programming
February 13, 2023 - In Java, methods are not first class objects. The closest we get is Java Lambda Expressions. I will not cover Java lambda expressions here, as I have covered them in both text and video in my Java Lambda expression tutorial. ... The execution of the function has no side effects. The return value of the function depends only on the input parameters passed to the function. Here is an example of a pure function (method) in Java:
Find elsewhere
🌐
W3Schools
w3schools.com › java › java_class_methods.asp
Java Class Methods
Remember that the name of the java file should match the class name. In this example, we have created two files in the same directory:
🌐
Baeldung
baeldung.com › home › java › core java › functional programming in java
Functional Programming in Java | Baeldung
January 2, 2026 - Traditionally, it was only possible to pass functions in Java using constructs such as functional interfaces or anonymous inner classes.
🌐
Learn Java
learnjavaonline.org › en › Functions
Functions - Learn Java - Free Interactive Java Tutorial
Which means we can call the method from a different class like that Main.foo(). void means this method doesn't return a value. Methods can return a single value in Java and it has to be defined in the method declaration. However, you can use return by itself to exit the method. This method doesn't get any arguments, but of course Java methods can get arguments as we'll see further on.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Get-the-most-from-Java-Function-interface-with-this-example
A simple Java Function interface example: Learn Functional programming fast
January 30, 2025 - When you run the inner class example, the output would be nine. Of course, the whole idea of the functional interface is to incorporate lambda expressions into the mix. Here’s the Java Function example implemented with a rather verbose lambda expression:
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › function › Function.html
Function (Java Platform SE 8 )
3 weeks ago - Returns a function that always returns its input argument. ... Java™ Platform Standard Ed. 8 ... Submit a bug or feature For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples...
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › How-to-use-Javas-functional-Consumer-interface-example
How to use Java's functional Consumer interface example
Sometimes programmers new to lambdas ... creating a class that implements java.util.function.Consumer, or by coding an inner class. Here is the Java code used in this Consumer function example tutorial....
🌐
freeCodeCamp
freecodecamp.org › news › functional-programming-in-java
Functional Programming in Java
January 20, 2026 - This article explores how to implement FP concepts in Java, including viewing functions as first-class citizens, chaining, and composing them to create function pipelines. We'll also discuss the technique of currying, which allows a function that takes multiple arguments to be transformed into a chain of functions that each take a single argument. This can simplify the use of complex functions and make them more reusable. In this article, I'll show you examples of how to implement these concepts in Java using modern language features, like “java.util.function.Function”, “java.util.function.BiFunction”, and a user-defined TriFunction.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Functions
Functions - JavaScript | MDN
Functions are one of the fundamental building blocks in JavaScript. A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is ...
🌐
Javabrahman
javabrahman.com › java-8 › java-8-java-util-function-function-tutorial-with-examples
Java 8 java.util.function.Function Tutorial with Examples
Java 8 code showing usage of static method Function.identity() //import statements are same as in apply() example public class FunctionTRIdentityExample{ public static void main(String args[]){ Function<Employee, String> funcEmpToString= (Employee e)-> {return e.getName();}; List<Employee> ...
🌐
Baeldung
baeldung.com › home › java › core java › functional interfaces in java
Functional Interfaces in Java | Baeldung
March 26, 2025 - Not all functional interfaces appeared in Java 8. Many interfaces from previous versions of Java conform to the constraints of a FunctionalInterface, and we can use them as lambdas. Prominent examples include the Runnable and Callable interfaces that are used in concurrency APIs.
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › functional-interface-in-java
Functional Interface in Java : A Complete Guide
Post Java 8, many interfaces were converted into Functional Interfaces, and these are annotated with @FunctionalInterface. These interfaces are given below. ... Primitive data types cannot be directly used as parameters for generic type arguments. For example, we cannot pass int, double, or float instead of T or R in R apply(T t) of Function ... In functional interfaces and Streams, certain classes are designed specifically to operate with int, long, and double without boxing them into wrapper classes.
🌐
Readthedocs
java-8-tips.readthedocs.io › en › stable › funcinterfaces.html
4. Functional Interfaces — Java 8 tips 1.0 documentation
Consumer<List<Integer>> c = Collections::sort; is an example of method reference for static methods. Compiler will automatically consider it as (list) -> Collections.sort(list). Here the target type will be the class name that contains the static method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-functional-interfaces
Java Functional Interfaces - GeeksforGeeks
November 20, 2025 - A functional interface in Java is an interface that has only one abstract method, making it suitable for use with lambda expressions and method references (introduced in Java 8). Use @FunctionalInterface to ensure only one abstract method (annotation is optional). Enable clean, concise code using lambdas and method references. Example: Using a Functional Interface with a Lambda Expression ... public class Geeks { public static void main(String[] args) { // Using lambda expression to implement Runnable new Thread(() -> System.out.println("New thread created")).start(); } }