{"id":599,"date":"2016-01-13T14:25:40","date_gmt":"2016-01-13T14:25:40","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?p=599"},"modified":"2016-01-13T14:25:41","modified_gmt":"2016-01-13T14:25:41","slug":"syntax","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/java\/syntax.html","title":{"rendered":"Syntax And Elements of Declaration With Example In Java"},"content":{"rendered":"<p>The\u00a0syntax\u00a0in java refers to the set of rules defining how a Java program is written and interpreted. The syntax is mostly derived from\u00a0C\u00a0and\u00a0C++.<\/p>\n<hr \/>\n<h4><strong><span style=\"color: #008000;\">Elements Of Java Syntax:<\/span><\/strong><\/h4>\n<p>A Java file can contain the following elements:<\/p>\n<ul>\n<li>Package declaration<\/li>\n<li>Import statements<\/li>\n<li>Type declaration<\/li>\n<li>Fields declaration<\/li>\n<li>Methods declaration<\/li>\n<li>Constructors declaration<\/li>\n<\/ul>\n<p><span style=\"color: #008000;\"><strong>Example of General JAVA Program Syntax:<\/strong><\/span><\/p>\n<p>Below is a program example that contains all of the above elements which will help you understand the basic syntax of a \u201c.java\u00a0file\u201d:<\/p>\n<pre>package basicsyntax; \/\/Package\r\n\r\nimport java.util.HashMap; \/\/Importing Statements\r\n\r\n\/\/Class\r\npublic class MyFirstClass {\r\n\r\n\u00a0\u00a0 \u00a0String name = \"Ram\"; \/\/Field\r\n\r\n\/\/ Constructor in JAVA\r\n\u00a0\u00a0 \u00a0public MyFirstClass() {\r\n}\r\n\r\n\/\/Method in JAVA\r\n\u00a0\u00a0 \u00a0public String getName()\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return this.name;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\/\/Static Main Method in JAVA\r\n\u00a0\u00a0 \u00a0public static void main(String[] args) {\r\n\r\n\u00a0\u00a0 \u00a0}\r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Explanation of all the above elements are given below:<\/strong><\/span><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>1. Package Declaration:<\/strong><\/span><\/p>\n<p>The first line of the program shows package declaration. This part, to be more specific:<\/p>\n<pre>package basicsyntax;<\/pre>\n<p>The package declaration consists of the word\u00a0package, a space, and then the name of the package. The\u00a0.java\u00a0file should be located in a directory structure that matches the package name.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>2. Import Statements:<\/strong><\/span><\/p>\n<p>A program can have single or multiple import statement depending on requirement. An import statement tells the Java compiler that this Java class is using other java class.<\/p>\n<pre>import java.util.HashMap;<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>3. Type Declaration:<\/strong><\/span><\/p>\n<p>Type declaration may be a <b>class<\/b>, an <b>abstract class<\/b>,\u00a0an <b>interface<\/b>, an <b>enum<\/b>\u00a0or an <b>annotation<\/b>.<\/p>\n<p>Here below type declared is a class. The type declaration is beginning with opening brace ({)and ending with closing brace(}) like shown in the program:<\/p>\n<pre>public class MyFirstClass {\r\n}<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>4. Field Declaration:<\/strong><\/span><\/p>\n<p>The field declaration ends with a semicolon(;). A type (class \/ interface \/ enum) can have more than one field.\u00a0 Below is one field declaration:<\/p>\n<pre>String name = \"Ram\";<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong><span style=\"text-decoration: underline;\">5. Constructors Declaration<\/span>:<\/strong><\/span><\/p>\n<p>A constructor declaration is for initialization of value assigned to class variables. A class can have more than one constructor:<\/p>\n<pre>public MyClass() {\r\n}<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>6. Methods Declaration:<\/strong><\/span><\/p>\n<p>When you create an instance of a class (an object) the object can have methods you can execute. These are also sometimes called &#8220;instance methods&#8221; because they require an instance of the object before you can call the method.\u00a0 Here is the method declaration from the example above:<\/p>\n<pre>public String getName() {\r\nreturn this.name;\r\n}<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>7. Main Static Method:<\/strong><\/span><\/p>\n<p>A static method belongs to the class, not objects of the class which means that you can call a static method without having an object of the class the static method belongs to.<\/p>\n<pre>public static void main(String[] args) {\r\n\r\n}<\/pre>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Importance Of Syntax:<\/strong><\/span><\/h4>\n<ul>\n<li>Syntax helps you in using or calling the function even though you do not know the whole implementation inside it.<\/li>\n<li>It helps the compiler in performing the cross check of the function called used in code, as it checks if the syntax matches with used one or not.<\/li>\n<li>It also helps in solving compiler error to some extent.<\/li>\n<\/ul>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Important Points To Remember:<\/strong><\/span><\/h4>\n<p>Following points should be keep in mind about java programs:<\/p>\n<ul>\n<li><strong>Case Sensitivity: &#8211;\u00a0<\/strong>Java is case sensitive, which means the uppercase and lowercase letters must be treated as different. For example, identifier\u00a0<strong>Cat <\/strong>and\u00a0<strong>cat\u00a0<\/strong>would have different meaning in Java.<\/li>\n<li><strong>Class Names: &#8211;\u00a0<\/strong>The first letter of a class name should be in Upper Case.\u00a0If a class name has several words then each inner word&#8217;s first letter should be in Upper Case. For example: <em>class MyFirstClass<\/em>.<\/li>\n<li><strong>Method Names: \u2013\u00a0<\/strong>The first letter of a method name should be in Lower Case.\u00a0If a method name has several words then each inner word&#8217;s first letter should be in Upper Case. For example: \u00a0<em>public void myGetMethod()<\/em><\/li>\n<li><strong>Program File Name \u2013\u00a0<\/strong>The class name and the program file name should be exactly same.\u00a0You should save program file using the class name (Remember Java is case sensitive) and add &#8216;.java&#8217; to the end of the file name when saving it. (if the file name and the class name is not same your program will not compile)<br \/>\nFor example: Suppose &#8216;MyFirstProgram&#8217; is the class name. Then the file should be saved as\u00a0<em>&#8216;MyFirstProgram.java&#8217;<\/em><\/li>\n<li><strong>public static void main(String args[]) &#8211;<\/strong>It is a mandatory part of every Java program that Java program processing starts from the main() method.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0syntax\u00a0in java refers to the set of rules defining how a Java program is written and interpreted. The syntax is mostly derived from\u00a0C\u00a0and\u00a0C++. Elements Of Java Syntax: A Java file can contain the following elements: Package declaration Import statements Type declaration Fields declaration Methods declaration Constructors declaration Example of General JAVA Program Syntax: Below is &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/syntax.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Syntax And Elements of Declaration With Example In Java<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,69],"tags":[],"class_list":["post-599","post","type-post","status-publish","format-standard","hentry","category-archieve","category-other"],"psp_head":"<title>Syntax And Elements of Declaration With Example In Java \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on Syntax in JAVA with example and explanation of program. The syntax in java refers to the set of rules defining how a Java program is written and interpreted.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/syntax.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=599"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/posts\/599\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/categories?post=599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/tags?post=599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}