Alex
Alex

Reputation: 68396

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…

Upvotes: 54

Views: 77742

Answers (5)

BillThor
BillThor

Reputation: 7576

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.

Modifiable static variables are risky. They act like a global variable, which can make the application fragile. Tracing where the variable was modified can be difficult.

Static methods are not risky. They can replace repeated code, increasing the likelihood the code will be well tested and correct.

Upvotes: 47

Usha Bhat
Usha Bhat

Reputation: 11

Example:

public class Methods_Test1 
{   
    public static void Display(String Name)
    {
        System.out.println("Hello There " + Name);
        System.out.println("I am from Display method");
    }


    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter name");
        String name = sc.next();
        Obj.Display(name);

    }

The public static void Display(String name) method accessible as static method within its own class which can be accessed without creating object of the class where as the same method behaves as public for the outside classes which can be accessed by creating object.

Upvotes: 0

zloctb
zloctb

Reputation: 11177

An example: when use the static keyword, we cannot use $this

class Foo {
    private $foo='private';

    private function priv_func() {
        echo 'priv_method';
    }

    public static function get() {
        echo $this->foo;
        $this->priv_func();
    }
}

$obj = new Foo();
$obj->get();

Fatal error: Using $this when not in object context in (…)

Upvotes: 5

bensiu
bensiu

Reputation: 25564

From http://php.net/manual/en/language.oop5.static.php

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

Upvotes: 17

Cristian David
Cristian David

Reputation: 694

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.

Upvotes: 43

Related Questions