Created a class as public class Library
Created a object as Library books = new Library();
Object is memory representation of class.
Most important object name should not be in capital.
How to create an object in Java? {TBD}
Java provides five ways to create an object.
Using new KeywordUsing clone() methodUsing newInstance() method of the Class classUsing newInstance() method of the Constructor classUsing DeserializationUsing new Keyword Using clone() method Using newInstance() method of the Class class Using newInstance() method of the Constructor class Using DeserializationUsing new Keyword Using clone() method Using newInstance() method of the Class class Using newInstance() method of the Constructor class Using Deserialization
Enter fullscreen mode Exit fullscreen mode
Code:
public class Library{public static void main(String[] args){Library books = new Library(); //Object Creation//new allocates memory - Instance - Instantiationbooks.fiction(); //Method Calling StatementLibrary librarian = new Library();librarian.lending_books();}public void fiction() //Method Signature{//Method Definition / BodySystem.out.println("Book Name:Fiction books");}public void lending_books() //Method Signature{//Method Definition / BodySystem.out.println("Librarian:Successfully lended the books");}}public class Library { public static void main(String[] args) { Library books = new Library(); //Object Creation //new allocates memory - Instance - Instantiation books.fiction(); //Method Calling Statement Library librarian = new Library(); librarian.lending_books(); } public void fiction() //Method Signature { //Method Definition / Body System.out.println("Book Name:Fiction books"); } public void lending_books() //Method Signature { //Method Definition / Body System.out.println("Librarian:Successfully lended the books"); } }public class Library { public static void main(String[] args) { Library books = new Library(); //Object Creation //new allocates memory - Instance - Instantiation books.fiction(); //Method Calling Statement Library librarian = new Library(); librarian.lending_books(); } public void fiction() //Method Signature { //Method Definition / Body System.out.println("Book Name:Fiction books"); } public void lending_books() //Method Signature { //Method Definition / Body System.out.println("Librarian:Successfully lended the books"); } }
Enter fullscreen mode Exit fullscreen mode
Output:
© 版权声明
THE END
暂无评论内容