Receiving Helpdesk

what is public void in java

by Miss Alison Wisoky II Published 2 years ago Updated 2 years ago

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".

Full Answer

Why is main public static void method in Java?

  • Public: It is an Access modifier, which specifies from where and who can access the method. ...
  • Static: It is a keyword which is when associated with a method, makes it a class related method. ...
  • Void: It is a keyword and used to specify that a method doesn’t return anything. ...
  • main: It is the name of Java main method. ...

More items...

What does 'public static void' mean in Java?

This means that you can call a static method without creating an object of the class. Void: Void means that the method has no return value. If the method returned an int you would write int instead of void. The line “public static void” in java is very important to understand because it is the core and basic concept in java.

What does "void" mean in Java?

void is a Java keyword. Used at method declaration and definition to specify that the method does not return any type, the method returns void . It is not a type and there is no void references/pointers as in C/C++.

Can We override a private or static method in Java?

You cannot override a private or static method in Java. If you create a similar method with same return type and same method arguments in child class then it will hide the super class method; this is known as method hiding. Similarly, you cannot override a private method in sub class because it’s not accessible there.

Why do we use public void main in Java?

It is a keyword and is used to specify that a method doesn't return anything. As the main() method doesn't return anything, its return type is void. As soon as the main() method terminates, the java program terminates too.Feb 10, 2022

What is public void run () used for?

Thread Class public void run() run(). run() method of the thread contains the executable code of the thread. This method is not static so we cannot access this method with the class name too. Thread class contains run() method with empty implementation.Jul 29, 2019

What is run () in Java?

The run() method of thread class is called if the thread was constructed using a separate Runnable object otherwise this method does nothing and returns. When the run() method calls, the code specified in the run() method is executed. You can call the run() method multiple times.

What is sleep method in Java?

The sleep() method is a static method of Thread class and it makes the thread sleep/stop working for a specific amount of time. The sleep() method throws an InterruptedException if a thread is interrupted by other threads, that means Thread.Aug 21, 2019

Why do we use void?

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).

What does "first public" mean in Java?

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.

What does static mean in Java?

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.

What is public method?

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.

Can you call a static method without creating an object?

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. Highly active question.

Can you call a static object without instantiating it?

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. Share.

Definition and Usage

The void keyword specifies that a method should not have a return value.

More Examples

Tip: If you want a method to return a value, you can use a primitive data type (such as int, char, etc.) instead of void, and use the return keyword inside the method:

1. Public

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.

2. Static

It is a keyword that is when associated with a method, making it a class-related method. The main () method is static so that JVM can invoke it without instantiating the class. This also saves the unnecessary wastage of memory which would have been used by the object declared only for calling the main () method by the JVM.

3. Void

It is a keyword and is used to specify that a method doesn’t return anything. As the main () method doesn’t return anything, its return type is void. As soon as the main () method terminates, the java program terminates too. Hence, it doesn’t make any sense to return from the main () method as JVM can’t do anything with the return value of it.

4. main

It is the name of the Java main method. It is the identifier that the JVM looks for as the starting point of the java program. It’s not a keyword.

image
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9