{"id":88612,"date":"2023-10-16T19:00:02","date_gmt":"2023-10-16T13:30:02","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=88612"},"modified":"2023-10-16T19:00:02","modified_gmt":"2023-10-16T13:30:02","slug":"java-object-and-class","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/","title":{"rendered":"Java Object and Class"},"content":{"rendered":"<p>In Java, An object is a physical as well as a logical entity. Whereas a class in Java is only a logical entity. Classes are used to create and manage objects and help to support inheritance.<\/p>\n<p>An object is a collection of related data\/functionality. Objects are created to share some behaviour. Objects can be created to call non-static methods which are not presented inside the Main Method but present inside the class.<\/p>\n<h2>Object<\/h2>\n<p>An object is the building block of the object-oriented programming language. An object is a real-world entity with its own attributes, behaviour and identity.<\/p>\n<p><strong>Real World Examples for objects<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/Real-World-Example-for-objects.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-88752\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/Real-World-Example-for-objects.webp\" alt=\"Real World Example for objects\" width=\"500\" height=\"185\" \/><\/a><\/p>\n<h3>Characteristics of object<\/h3>\n<p><strong> An object has three characteristics:<\/strong><\/p>\n<ul>\n<li><strong>STATE:<\/strong> It represents the data or value of an object.<\/li>\n<li><strong>BEHAVIOUR:<\/strong> It represents the functionality or methods of an object.<\/li>\n<li><strong>IDENTITY:<\/strong> Each object has a unique identity. Each object is uniquely identifiable.<\/li>\n<\/ul>\n<p><strong>Example of an Object<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/Example-of-an-Object.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-88753\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/Example-of-an-Object.webp\" alt=\"Example of an Object\" width=\"500\" height=\"338\" \/><\/a><\/p>\n<h4>How to initialise the object in the class<\/h4>\n<p><strong>Objects can be initialised in three ways.<\/strong><\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/How-to-initialise-object-in-class.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-88754\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2023\/09\/How-to-initialise-object-in-class.webp\" alt=\"initialise object in class\" width=\"500\" height=\"266\" \/><\/a><\/p>\n<h3>Class<\/h3>\n<p>In Java, A class is a fundamental building block of Object-oriented programming(OOP) language. A class can define the structure, behaviour and state of an Object.<\/p>\n<p><strong> A class can be defined in two ways :<\/strong><\/p>\n<ul>\n<li>Class expression.<\/li>\n<li>Class declaration.<\/li>\n<\/ul>\n<h4>How to declare a class in Java<\/h4>\n<p>We can declare a class using the \u201cclass\u201d keyword in front of the class name, followed by the curly braces.<\/p>\n<p><strong>Class declaration<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Public class Dataflair{\n\/\/ body of the class(field and method)\n}<\/pre>\n<p><strong>Create a class<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">Create a class named \u2018DataFlair\u201d with String S\n    public class DataFlair {\n     \t\tint x =6;\n     \t  In this example, we will create a class named with \u201cDataFlair\u201d<\/pre>\n<p><strong>Create an object<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">  Create an object called \u201cobj\u201d and print the value of S\npublic class DataFlair {\n        int x = 6;\n        public static void main(String[] args) {\n            DataFlair obj = new DataFlair();\n        System.out.println(obj.x);\n    }\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>6<\/p>\n<p>In the above program, we have created a class named \u201cDataFlair,\u201d which contains a variable x. Inside a class, we have created an object named \u201cobj\u201d, and then we use the objects to call the methods of a class.<\/p>\n<h3>Multiple objects<\/h3>\n<p>We can create more than one object in one class. <strong>For example:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class DataFlair{\nInt x = 6;\nint y = 8;\nString s = \u201cData Flair\u201d;\nPublic static void main(String args[])\n{\nDataFlair obj1 = new DataFlair();\nDataFlair obj2 = new DataFlair();\nDataFlair obj3 = new DataFlair();\nSystem.out.println(obj1.x);\nSystem.out.println(obj2.y);\nSystem.out.println(obj3.s);\n}\n}<\/pre>\n<p><strong>Output :<\/strong><\/p>\n<p>6<br \/>\n8<br \/>\nData Flair<\/p>\n<p>In the above program, we have created a class named \u201cDataFlair.\u201d it contains 3 variables: x, y and s. Inside a class, we have created an object named \u201cobj1\u201d, \u201cobj2\u201d, and \u201cobj3.\u201d Then, we used the objects to call the methods of a class.<\/p>\n<h3>Difference between classes and object<\/h3>\n<table>\n<tbody>\n<tr>\n<td><span style=\"font-weight: 400\">CLASS<\/span><\/td>\n<td><span style=\"font-weight: 400\">OBJECT<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">A template for creating objects within a program.<\/span><\/td>\n<td><span style=\"font-weight: 400\">An instance of a class.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Logical entity.<\/span><\/td>\n<td><span style=\"font-weight: 400\">Physical entity.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">Declared with the \u201cclass\u201d keyword.<\/span><\/td>\n<td><span style=\"font-weight: 400\">Created using the \u201cnew\u201d keyword<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">A class is declared once.<\/span><\/td>\n<td><span style=\"font-weight: 400\">Multiple objects are created using a class.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">A class does not have memory when it is created.\u00a0<\/span><\/td>\n<td><span style=\"font-weight: 400\">Objects get memory when they are created.<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Java, An object is a physical as well as a logical entity. Whereas a class in Java is only a logical entity. Classes are used to create and manage objects and help to&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":88661,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[183],"tags":[1618,296,5226,5227,5228],"class_list":["post-88612","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-classes-and-objects-in-java","tag-java","tag-java-object-and-class","tag-object-and-class","tag-what-is-classes-and-objects-in-java"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Object and Class - TechVidvan<\/title>\n<meta name=\"description\" content=\"In Java Object and Class, An object is a physical as well as a logical entity. Whereas a class in Java is only a logical entity.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Object and Class - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"In Java Object and Class, An object is a physical as well as a logical entity. Whereas a class in Java is only a logical entity.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-16T13:30:02+00:00\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Object and Class - TechVidvan","description":"In Java Object and Class, An object is a physical as well as a logical entity. Whereas a class in Java is only a logical entity.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/","og_locale":"en_US","og_type":"article","og_title":"Java Object and Class - TechVidvan","og_description":"In Java Object and Class, An object is a physical as well as a logical entity. Whereas a class in Java is only a logical entity.","og_url":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2023-10-16T13:30:02+00:00","author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Java Object and Class","datePublished":"2023-10-16T13:30:02+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/"},"wordCount":404,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/#primaryimage"},"thumbnailUrl":"","keywords":["Classes and Objects in Java","java","java object and class","object and class","what Is classes and objects in java"],"articleSection":["Java Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/","url":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/","name":"Java Object and Class - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/#primaryimage"},"thumbnailUrl":"","datePublished":"2023-10-16T13:30:02+00:00","description":"In Java Object and Class, An object is a physical as well as a logical entity. Whereas a class in Java is only a logical entity.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/#primaryimage","url":"","contentUrl":""},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/java-object-and-class\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Java Object and Class"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/88612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=88612"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/88612\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=88612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=88612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=88612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}