hey guy i have adoubt regarding public void static i mean im not able to get when to write at that time ith public void......... so can someone help me

The "void" in c++ means this function returns nothing.

The "public" in oop languages such as c++, php, c# means access to the member declared is available outside of the class functions.

The "static" in oop languages such as c++, php, c# means VALUE of the member declared is common in all instances of the class and is available by :: ( className::staticMember).

Edited 2 Years Ago by returnzero: grammer

if i understand your question correctly, you are wondering when to use the private/public keywords, when to use void as a return type and when to use the static keyword when declaring functions.

so lets break that down;

public/private simply decides who gets to call the function you are writing. These modifiers are can also be used when declaring a variable.

A simple example of its use would be to create get and set public methods to access a private variable.

//code not tested, proof of concept purpose only.

//outside of the current class, the variable iBalance would be unaccessible.
private int iBalance;

//public method to check iBalance's value from outside of current class.
public int getBalance(){
    return iBalance;
}

//public method to set iBalance's value from outside of current class,
//with the condition that the value is above zero.
public void setBalance(int value){
    if(value > 0){
        iBalance = value;
    }
}

void is used as the return type of a method that does not return a value.

With the exception of constructors, all methods need a return type. When the actions your method achieves do not require it to return a value, you are still expected to define void as its return type.

//code not tested, proof of concept purpose only.

//method with the return type "int" because it returns an int value.
public int getBalance(){
    return iBalance;
}

//method with the return type "void" because it does not return a value.
public void resetBalance(){
    iBalance = 0;
}

Static is a keyword that allows you to declare methods or variables for the class rather than for the instances of the class. When something is not specific to an instance but generic for every instances of a certain class, it should be declared as static.

//code not tested, proof of concept purpose only.

public class Car{
    private static int iNumCars = 0;
    private string sColor;

    public Car(string color){
        sColor = color;
        if()
        iNumCars++;
    }

    public string getColor(){
        return sColor;
    }

    public setColor(string value){
        sColor = value;
    }

    public static int getNumCars(){
        return iNumCars;
    }
}

//later to use the Car class somewhere else :
public static void main(...)
{
    //create an instance of car
    public Car myCarInstance = new Car("Blue");

    //print the car's color by calling the method from the car instance
    System.out.println( "This car is " + myCarInstance.getColor() );

    //print the number of cars created from the "class method" (static)
    System.out.println( "There was " + Car.getNumCars() + "cars created." );
}

notice that static methods (or variables) are called from the class itself, while regular methods and variables are called from an instance of the class.

myInstance.regularMethod(); vs myClass.staticMethod();

and thats pretty much it, the links i provided will cover these notions better if you want to go deeper into them.

Happy coding, hope this helped!

edit : damnit, got beat to first reply! :p

Edited 2 Years Ago by Philippe.Lahaie

This article has been dead for over six months. Start a new discussion instead.