Home » Core Java » class » Passing an object to a method example

About Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Passing an object to a method example

With this example we are going to demonstrate how to pass an object to a method. In short, we have created a class and a method to pass the object of the class:

  • We have created a class, Character with a char field.
  • We have created another class, PassObject, that has a static method, void setCh(Character y). The method gets a Character object and changes its char field to 'z'.
  • We create a new instance of Character class and set its char field to 'a'.
  • Then we use the setCh(Character y) method of PassObject class to change again the field of Character object.
  • In both ways the field of Character object is changed.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.core;

class Character {
    char ch;

public class PassObject {

    static void setCh(Character y) {

  y.ch = 'z';

    public static void main(String[] args) {

  Character x = new Character();

  x.ch = 'a';

  System.out.println("1: x.ch: " + x.ch);


  System.out.println("2: x.ch: " + x.ch);


1: x.ch: a
2: x.ch: z

This was an example of how to pass an object to a method in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments