Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
  • Register
  • Login
  1. Home
  2. Blogpost

User-defined data types in java

Date- Sep 05,2023

6140

User-defined data types in java

In Java, you can create user-defined data types by defining your own classes. These classes allow you to encapsulate data and behavior into custom data structures. Here's how you can create a user-defined data type in Java:

1. Define a Class: To create a user-defined data type, you need to define a class. The class represents the blueprint or template for objects of that type. Here's a simple example of a Person class:

package Tutorial_01;

public class Person {
	// Fields or instance variables
    private String name;
    private int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Methods
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void sayHello() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }
}

1. Create Objects: Once you have defined a class, you can create objects (instances) of that class using the new keyword. For example:

package Tutorial_01;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		 // Create Person objects
        Person person1 = new Person("Alliya", 35);
        Person person2 = new Person("john", 40);

        // Access fields and methods
        System.out.println(person1.getName()); // Output: Alliya
        System.out.println(person2.getAge());  // Output: 40

        person1.sayHello(); // Output: Hello, my name is Alliya and I am 35 years old.
	}

}


In this example, we've created a Person class with fields (name and age), a constructor to initialize those fields, and methods (getName, getAge, and sayHello) to access and manipulate the data.

By defining your own classes, you can create custom data types that suit your application's specific needs and encapsulate related data and behavior within those classes. This is one of the fundamental principles of object-oriented programming (OOP) in Java.

S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Comments

Tags

Swagger UI
Swashbuckle
SwashbuckleAspNetCore
Rest API
Postman
Api Testing
ITextSharp
Export to Pdf
AspNet Core
AspNet
C#
View to Pdf in Aspnet
Scheduler
Fibonacci series in Java
Display Fibonacci Series
First C# Program
What is C?
C
C Programming
CodeLobster
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, India   info@code2night.com

Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • XML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • PDF Editor
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Built with for developers