Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between "public" and "public static"?

What does static mean?

I know public means that it can be accessed from outside the class, and private only from inside the class…

like image 667
Alex Avatar asked Apr 03 '11 02:04

Alex


People also ask

What is the difference between public static and public?

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 difference between public method and public static method?

public methods and properties are accessible only after instantiating class and is called via "->" sign. public static methods and properties can be accessed without need of instantiating class and can be called via "::".

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

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 is difference between public and static in Java?

public : It is an access specifier, which defines who can access this method. public access means this method can be accessed by any class (if other classes are able to access this class, in which the public method is defined). static : It is a keyword that makes sure that statically declared method is class level.


2 Answers

Static means that it can be accessed without instantiating a class. This is good for constants.

Static methods need to have no effect on the state of the object. They can have local variables in addition to the parameters.

like image 186
BillThor Avatar answered Oct 15 '22 18:10

BillThor


public: Public declared items can be accessed everywhere.

protected: Protected limits access to inherited and parent classes (and to the class that defines the item).

private: Private limits visibility only to the class that defines the item.

static: A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

final: Final keywords prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.


Beside of PHP:

transient: A transient variable is a variable that may not be serialized.

volatile: A variable that might be concurrently modified by multiple threads should be declared volatile. Variables declared to be volatile won't be optimized by the compiler because their value can change at anytime.

like image 32
Cristian David Avatar answered Oct 15 '22 18:10

Cristian David