- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 a method that has a parameter in Java
A class declaration can contain a method that has a parameter in Java. A program that demonstrates this is given as follows:
Example
class Message { public void messagePrint(String msg) { System.out.println("The message is: " + msg); } } public class Demo { public static void main(String args[]) { Message m = new Message(); m.messagePrint("Java is fun"); } }
Output
The message is: Java is fun
Now let us understand the above program.
The Message class is created with a single member function messagePrint() that has a parameter msg i.e. the message to be printed. A code snippet which demonstrates this is as follows:
class Message { public void messagePrint(String msg) { System.out.println("The message is: " + msg); } }
In the main() method, an object m of class Message is created. Then messagePrint() method is called with string value "Java is fun". A code snippet which demonstrates this is as follows:
public class Demo { public static void main(String args[]) { Message m = new Message(); m.messagePrint("Java is fun"); } }
- Related Articles
- Class declaration with one method in Java
- Why the main method has to be in a java class?
- How to ensure that child class overrides a super class method in java?
- What is a method in Java that ends in a semicolon and has no method body?
- Does Java support default parameter values for a method?
- How to pass a lambda expression as a method parameter in Java?
- JavaScript focus a particular element with div class, not div id declaration?
- Can abstract method declaration include throws clause in java?
- Load class with forName() method in Java
- Override the toString() method in a Java Class
- Set tuple as a method parameter in C#
- Passing empty parameter to a method in JavaScript
- Pass long parameter to an overloaded method in Java
- What is the syntax for passing Scanner object as a parameter in a method using java?
- What is a final parameter in java?

Advertisements