Different between public, static, and void?
Loading
Different between public, static, and void?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sam HobbsPosted Oct 9, 2022, 5:55 AM
Note that static can be a modifier and it can be a directive. Public is an access modifier. When everything is called a keyword it is more difficult to know what everything is.
Abhishek YadavPosted Oct 19, 2022, 8:35 AM
Public − This is the get right of entry to specifier that states that the approach may be accesses publically. static − Here, the item isn't always required to get right of entry to static members. void − This states that the approach doesn’t go back any value.
Brahma Prakash ShuklaPosted Oct 18, 2022, 8:23 AM
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.
Brahma Prakash ShuklaPosted Oct 17, 2022, 7:54 AM
Here is the difference-
public − This is the access specifier that states that the method can be accessed publically.Whenever a method, class or a class member declared with public access specifier. it means that the member can be access from any where. There is no restriction on accessing the public member.
static − Here, the object is not required to access static members.If a class is declared us Public we can access the class wherever we want.
if a member of the class is public then we can access the member from anywhere there wont be any restriction on this but class has it restriction if the class isn’t declared public.
Static keyword is used to mention that the static member value won’t change in anytime). throughout the application cycle static member gives same value with referring same memory location.
void − This states that the method doesn’t return any value.
if a variable is declared with static keyword and assigned so some value. While executing a memory blocks get assigned to the static variable. We can able to access the static members without any instance. We can access the member from any where in the application it will same value referring to same memory location.
Void keyword is used in the return type. which actually stating that the method with void return type wont return anything (it returns nothing. don’t think nothing as null because null is a value).
Sachin SinghPosted Oct 9, 2022, 3:34 PM
100% agree with Sam Hobbs sir.
Vishal YelvePosted Oct 9, 2022, 3:26 PM
Public, Static and Void comes under different categories. Public is the access specifier, static is a keyword and void comes under return type. let me explain one by one:
Public: Whenever a method, class or a class member declared with public access specifier. it means that the member can be access from any where. There is no restriction on accessing the public member.
If a class is declared us Public we can access the class wherever we want.
if a member of the class is public then we can access the member from anywhere there wont be any restriction on this but class has it restriction if the class isn’t declared public.
Static keyword is used to mention that the static member value won’t change in anytime). throughout the application cycle static member gives same value with referring same memory location.
if a variable is declared with static keyword and assigned so some value. While executing a memory blocks get assigned to the static variable. We can able to access the static members without any instance. We can access the member from any where in the application it will same value referring to same memory location.
Void keyword is used in the return type. which actually stating that the method with void return type wont return anything (it returns nothing. don’t think nothing as null because null is a value).
Rajanikant HawaldarPosted Oct 8, 2022, 6:19 AM
The Public access modifier states that any variable that is defined using public can be accessed anywhere
in the class and it will be also available when class is object is created and when do class inheritence means
you can access public declared variables anywhere in the application. When you specify class as public, the class
is visible outside the assembly.
Example of using Public access modifier:
The Static modifier declared variables are globally accessible without creating an instance of the class and it will be accessed class level. If static is used with class then it can not be instanciated. Static items can only access other static items. For example, a static class can only contain static members, e.g. variable, methods etc. if we mark method as static then it can only contain static variables and can only access other static items. Static items share the resources between multiple users. Static cannot be used with indexers, destructors or types other than classes. A static constructor in a non-static class runs only once when the class is instantiated for the first time. A static constructor in a static class runs only once when any of its static members accessed for the first time. Static members are allocated in a high frequency heap area of the memory.
Example of using Static modifier:
The Void is a type modifier that specifies that the method doesn’t return any value but if we use return keyword in void method it just returns control back from this method and no further code execute in this method, refer below example.
Example of using Void type modifier: