0

Can someone please explain what both of these mean and the good details about them. Please also explain the parameters of how to use it.

2
Contributors
2
Replies
5
Views
7 Years
Discussion Span
Last Post by llemes4011
0

Hello.
Adding the private modifier to a method means that you can ONLY call it from within that class.
The public modifier means that you can access that method with the dot (.) operator from other classes.

public class Foo{
    public Foo{
        doSomethingInternal(); // Legal (This is the same as this.doSomethingInternal();
        doSomethingExternal(); // Legal (This is the same as this.doSomethingExternal();
    }
    public void doSomethingExternal(){/*code*/}
    private void doSomethingInternal(){/*code*/}
}
public class Bar{
    public static void main(String[] args){
        Foo foo = new Foo();
        foo.doSomethingExternal(); // Legal
        foo.doSomethingInternal(); // NOT Legal
    }
}

Hope that clears things up =)

Edited by llemes4011: correction

0

Good points about the public modifier - You can modify variables in the class through gettor/settor methods. These methods can be called by using the dot (.) operator.
Good points about the private modifier - It allows methods that should ONLY BE CALLED BY THE CLASS (initing vars, handling events, ect) to be non-accessible to anything outside the class. These methods cannot be called by using the dot (.) operator.

(Sorry for double post)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.