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.

Recommended Answers

All 2 Replies

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 =)

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.