You have already declared it. The thing you are missing is your function body.

public static void main(String[] args)

should be

public static void main(String[] args){
   //DO Some Stuff
}

Now here is some additional info: The main function will be started whenever the application is started and the

String[] args

are the arguments that you are going to pass while starting the application. You can declare as many functions as you want within your class

public class demo{
    public static void main(String[] args){
      //Do Some Stuff
    }
    private void someFunction(){
      //Do Some Stuff
    }
}

For more you can start learning some basics from the internet. There are tons of tutorials. Hope that helps. :)

Answer from Shobuj on Stack Overflow
🌐
Medium
medium.com › javarevisited › looking-at-a-java-class-and-its-methods-through-a-kaleidoscope-998b510e39ac
Looking at a Java Class and its Methods Through a Kaleidoscope | Javarevisited
April 6, 2024 - Feature-rich interfaces, sometimes referred to as humane interfaces, are not incredibly humane when you need to find a method, if you don’t know the name, or discover patterns and look for symmetry between types. Thankfully, we have computers to help us process large amounts of data very quickly. Class and Method declarations are an easily accessible treasure trove of information about libraries and applications that Java developers have at their fingertips.
🌐
W3Schools
w3schools.com › java › java_class_methods.asp
Java Class Methods
To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon (;). A class must have a matching filename (Main and Main.java).
Discussions

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
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
🌐
Programiz
programiz.com › java-programming › methods
Java Methods (With Examples)
August 26, 2024 - Note: The method is not static. Hence, we are calling the method using the object of the class. A Java method may or may not return a value to the function call. We use the return statement to return any value.
🌐
DataCamp
datacamp.com › doc › java › class-methods
Java Class Methods
In Java, class methods are functions defined within a class that describe the behaviors or actions that objects of the class can perform. These methods can manipulate object data, perform operations, and return results.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › methods.html
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Typically, a method has a unique name within its class. However, a method might have the same name as other methods due to method overloading. The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures.
Find elsewhere
🌐
W3Schools
w3schools.com › java › java_classes.asp
Java Classes and Objects
You can also create an object of a class and access it in another class. This is often used for better organization of classes (one class has all the attributes and methods, while the other class holds the main() method (code to be executed)). Remember that the name of the java file should match the class name.
🌐
GeeksforGeeks
geeksforgeeks.org › java › methods-in-java
Java Methods - GeeksforGeeks
Declared inside class with static keyword. ... It consists of the method name and a parameter list. ... Note: The return type and exceptions are not considered as part of it. ... In Java language method name is typically a single word that should be a verb in lowercase or a multi-word, that begins with a verb in lowercase followed by an adjective, noun.
Published   3 weeks ago
🌐
Ars OpenForum
arstechnica.com › forums › operating systems & software › programmer's symposium
Java - passing a method to a class | Ars OpenForum
April 4, 2024 - But I wouldn't call myself good at them, much less figure out which part is a stream and which part is passing a function pointer. I'm still curious about how to do it in Java but as I've solved my original problem it's on a back burner.. ... $ cat Sample.java class Sample { public static int foo(java.util.function.Function<Integer,Integer> f, int value) { return f.apply(value); } public static class OtherFunction implements java.util.function.Function<Integer,Integer> { public Integer apply(Integer i) { return i * 3; } } public static void main(String[] args) { java.util.function.Function<Integer,Integer> x = (n) -> n*2; System.out.println(foo(x, 2)); System.out.println(foo(new OtherFunction(), 3)); } } $ javac Sample.java $ /usr/lib/jvm/jre-22/bin/java Sample 4 9 java.util.function.Function is an interface that defines a single abstract method.
🌐
Learn Java
learnjavaonline.org › en › Functions
Functions - Learn Java - Free Interactive Java Tutorial
In Java, all function definitions must be inside classes. We also call functions methods.
🌐
Tutorialspoint
tutorialspoint.com › java › java_class_methods.htm
Java - Class Methods
To access a class method (public class method), you need to create an object first, then by using the object you can access the class method (with the help of dot (.) operator). Use the below syntax to access a Java public class method:
🌐
Reddit
reddit.com › r/learnprogramming › java: when to use method and when to create a class
r/learnprogramming on Reddit: Java: When to use method and when to create a class
December 5, 2017 -

So I know that creating a class will be more secure.

When should I just go ahead and create a method in my 'main' class, and when should I create a whole new class to then invoke from my 'main' class?

Is it only to prevent users from messing up my program?

or is there a 'aesthetic' reason for doing this?

I'm not trying to program a huge project as i'm just a student beginning to learn this. My project is really just solving a given maze.

Top answer
1 of 5
6
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.
2 of 5
4
Welcome to the world of object-oriented programming :) The answer to your question is an entire programming principle in and of itself. I will try to ELI5 a shortened version of this for you, as you are just starting out. Off the top of your head, look around the room you're in and pick any item you see. Let's pick your computer, since I'm fairly sure that's what you've used to post this question. How can you describe computers, in general? What do they all have in common? Well, you could say they've all got an operating system on them (let's pretend they can only have one, for simplicity). They've all got a make, like "Apple" or "Google", and likely a model, like "Macbook Pro" or "Chromebook". They all have a year that they were released. A screen size. A price. And what about features, like an optical drive, audio speakers, a webcam, or a lack thereof? You can think of a class as an abstract description of a generic thing, like a computer. The name of your class should be the name of the thing. You've likely learned about variables in your main class - other classes can have their own variables that belong to them, and those are like the descriptors of your computer, like price or model. Other classes can have their own methods too, and those are like questions you can ask about the item, or actions you can perform on them. "What is the computer's price?" "Set the computer's operating system to MacOS X." When you create a new instance of a class, you're creating a new object with its own description, filling out all of those little details. The computer you're on right now has its own specific details and its own answers to those questions. The machine you're using is an instance of the Computer class. The machine I'm using is another instance. But they're both computers. So, your main class, with its main method, make up the starting point of your program. But, when you want to create a separate description of an item to work with, and keep it separated from your main code and make your project more organized than just putting everything into one class in one file, you should create a new class, and create an instance of that class in your main one (or wherever else you need it). In Java, all of your code should basically be made up of classes interacting with each other.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › function › Function.html
Function (Java Platform SE 8 )
3 weeks ago - All Classes · Summary: Nested | Field | Constr | Method · Detail: Field | Constr | Method · compact1, compact2, compact3 · java.util.function · Type Parameters: T - the type of the input to the function · R - the type of the result of the function · All Known Subinterfaces: UnaryOperator<T> Functional Interface: This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
🌐
Mkyong
mkyong.com › home › java8 › java 8 function examples
Java 8 Function Examples - Mkyong.com
February 27, 2020 - package com.mkyong; import java.util.function.Function; public class Java8Function2 { public static void main(String[] args) { Function<String, Integer> func = x -> x.length(); Function<Integer, Integer> func2 = x -> x * 2; Integer result = func.andThen(func2).apply("mkyong"); // 12 System.out.println(result); } } Output ·
🌐
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 default method Function.compose() //import statements are same as in apply() example public class FunctionTRComposeExample{ public static void main(String args[]){ Function<Employee, String> funcEmpToString= (Employee e)-> {return e.getName();}; Function<Employee, Employee> funcEmpFirstName= (Employee e)-> {int index= e.getName().indexOf(" "); String firstName=e.getName().substring(0,index); e.setName(firstName); return e;}; List<Employee> employeeList= Arrays.asList(new Employee("Tom Jones", 45), new Employee("Harry Major", 25), new Employee("Ethan Hardy", 65), ne
🌐
GeeksforGeeks
geeksforgeeks.org › java › function-interface-in-java
Function Interface in Java - GeeksforGeeks
July 11, 2025 - // Java Program to Illustrate Functional Interface // Via apply() method // Importing interface import java.util.function.Function; // Main class public class Geeks { // Main driver method public static void main(String args[]) { // Function which takes in a number // and returns half of it Function<Integer, Double> half = a -> a / 2.0; // Applying the function to get the result System.out.println(half.apply(10)); } } Output ·
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Class.html
Class (Java Platform SE 8 )
3 weeks ago - There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent, namely boolean, byte, char, short, int, long, float, and double.
🌐
Sololearn
sololearn.com › en › Discuss › 2291212 › difference-between-method-and-class-in-java
Difference between method and class in java? | Sololearn: Learn to code for FREE!
May 13, 2020 - Everything in Java is stored in a class (Not including imports and packages). Methods are code that can be called at anytime in that class.
🌐
Medium
medium.com › javaguides › java-function-functional-interface-with-real-world-examples-a1e86ea0fda5
Java Function Functional Interface with Real-World Examples | by Ramesh Fadatare | JavaGuides | Medium
March 1, 2025 - In Java functional programming, the Function<T, R> interface (from java.util.function) represents a function that takes one input and returns a result.