Creating Objects

To access the class fields and methods outside the defining class, an object needs to be created from the class.

The new operator is used to create an object. Memory is assigned for the object once its been created.

Using the following Socket class definition,

public class Socket {
    String url;
    void connect(){

    }
}

we can create a new socket object from the Socket class as follows

Socket socket = new Socket();

and we can call the connect method on the newly created object as follows :

socket.connect();

and we can set the url field as follows :

socket.url = "http://chat.peruzal.co.za";

results matching ""

    No results matching ""