- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Class declaration with one method in Java
A class declaration can contain a single method. A program that demonstrates this is given as follows:
Example
class Message { public void messagePrint() { System.out.println("This is a class with a single method"); } } public class Demo { public static void main(String args[]) { Message m = new Message(); m.messagePrint(); } }
Output
This is a class with a single method
Now let us understand the above program.
The Message class is created with a single member function messagePrint(). A code snippet which demonstrates this is as follows −
class Message { public void messagePrint() { System.out.println("This is a class with a single method"); } }
In the main() method, an object m of class Message is created. Then messagePrint() method is called. A code snippet which demonstrates this is as follows −
public class Demo { public static void main(String args[]) { Message m = new Message(); m.messagePrint(); } }
- Related Articles
- Class declaration with a method that has a parameter in Java
- Load class with forName() method in Java
- Can abstract method declaration include throws clause in java?
- java variable declaration
- Is it possible to override a Java method of one class in same?
- Method of Object class in Java
- NavigableSet Class floor() method in Java
- NavigableSet Class higher() method in Java
- NavigableSet Class lower() method in Java
- NavigableSet Class ceiling() method in Java
- How to set listStyleImage, listStylePosition, and listStyleType in one declaration with JavaScript?
- How to set all the outline properties in one declaration with JavaScript?
- JavaScript focus a particular element with div class, not div id declaration?
- How to set all the background properties in one declaration with JavaScript DOM?
- How to set all the border left properties in one declaration with JavaScript?

Advertisements