What is public class and public static void main?

What does public static void mean in Java?

I'm in the process of learning. In all the examples in the book I'm working from public static void comes before any method that is being used or created. What does this mean?

asked Mar 5, 2010 at 21:27

0

It's three completely different things:

public means that the method is visible and can be called from other objects of other types. Other alternatives are private, protected, package and package-private. See here for more details.

static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class.

void means that the method has no return value. If the method returned an int you would write int instead of void.

The combination of all three of these is most commonly seen on the main method which most tutorials will include.

What is public class and public static void main?

answered Mar 5, 2010 at 21:29

Mark ByersMark Byers

785k188 gold badges1552 silver badges1440 bronze badges

3

The three words have orthogonal meanings.

public means that the method will be visible from classes in other packages.

static means that the method is not attached to a specific instance, and it has no "this". It is more or less a function.

void is the return type. It means "this method returns nothing".

answered Mar 5, 2010 at 21:30

Thomas PorninThomas Pornin

71.8k14 gold badges146 silver badges188 bronze badges

0

The public keyword is an access specifier, which allows the programmer to control the visibility of class members. When a class member is preceded by public, then that member may be accessed by code outside the class in which it is declared. (The opposite of public is private, which prevents a member from being used by code defined outside of its class.)

In this case, main( ) must be declared as public, since it must be called by code outside of its class when the program is started.

The keyword static allows main( ) to be called without having to instantiate a particular instance of the class. This is necessary since main( ) is called by the Java interpreter before any objects are made.

The keyword void simply tells the compiler that main( ) does not return a value. As you will see, methods may also return values.

khr055

28.4k16 gold badges35 silver badges47 bronze badges

answered Sep 4, 2012 at 18:02

utsavutsav

3813 silver badges3 bronze badges

0

It means that:

  • public - it can be called from anywhere
  • static - it doesn't have any object state, so you can call it without instantiating an object
  • void - it doesn't return anything

You'd think that the lack of a return means it isn't doing much, but it might be saving things in the database, for example.

answered Mar 5, 2010 at 21:30

Paul TomblinPaul Tomblin

176k58 gold badges314 silver badges401 bronze badges

It means three things.

First public means that any other object can access it.

static means that the class in which it resides doesn't have to be instantiated first before the function can be called.

void means that the function does not return a value.

Since you are just learning, don't worry about the first two too much until you learn about classes, and the third won't matter much until you start writing functions (other than main that is).

Best piece of advice I got when learning to program, and which I pass along to you, is don't worry about the little details you don't understand right away. Get a broad overview of the fundamentals, then go back and worry about the details. The reason is that you have to use some things (like public static void) in your first programs which can't really be explained well without teaching you about a bunch of other stuff first. So, for the moment, just accept that that's the way it's done, and move on. You will understand them shortly.

What is public class and public static void main?

StudioTime

21.6k37 gold badges114 silver badges205 bronze badges

answered Mar 5, 2010 at 21:32

AaronAaron

1,0626 silver badges16 bronze badges

Considering the typical top-level class. Only public and no modifier access modifiers may be used at the top level so you'll either see public or you won't see any access modifier at all.

`static`` is used because you may not have a need to create an actual object at the top level (but sometimes you will want to so you may not always see/use static. There are other reasons why you wouldn't include static too but this is the typical one at the top level.)

void is used because usually you're not going to be returning a value from the top level (class). (sometimes you'll want to return a value other than NULL so void may not always be used either especially in the case when you have declared, initialized an object at the top level that you are assigning some value to).

Disclaimer: I'm a newbie myself so if this answer is wrong in any way please don't hang me. By day I'm a tech recruiter not a developer; coding is my hobby. Also, I'm always open to constructive criticism and love to learn so please feel free to point out any errors.

What is public class and public static void main?

answered Aug 25, 2012 at 16:06

1

Public - means that the class (program) is available for use by any other class.

Static - creates a class. Can also be applied to variables and methods,making them class methods/variables instead of just local to a particular instance of the class.

Void - this means that no product is returned when the class completes processing. Compare this with helper classes that provide a return value to the main class,these operate like functions; these do not have void in the declaration.

answered Mar 6, 2011 at 19:11

  • public means you can access the class from anywhere in the class/object or outside of the package or class
  • static means constant in which block of statement used only 1 time
  • void means no return type

gnat

6,210104 gold badges53 silver badges73 bronze badges

answered Jan 16, 2012 at 13:37

2

static means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class. Because of use of a static keyword main() is your first method to be invoked.. static doesn't need to any object to instance... so,main( ) is called by the Java interpreter before any objects are made.

What is public class and public static void main?

answered Jan 8, 2014 at 6:08

mahimahi

415 bronze badges

What is public in public static void main?

It is an Access modifier, which specifies from where and who can access the method. Making the main() method public makes it globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class.

What is public class and public void?

A class is a collection of methods and data. Public means that the class can be accessed by any other class in any package. main is a method of the class. Static means it is defined as a member of the class rather than a member of an instance of the class. Void means it returns nothing.

What is the difference between public static void main and static void Main?

public − This is the access specifier that states that the method can be accesses publically. static − Here, the object is not required to access static members. void − This states that the method doesn't return any value.

What is the difference between public void and public static void?

Public void : Used when you don't have to create an object and have no return. Public static void: Used when you need to create an object in the class itself .