Sunday, August 16, 2015

How to Declare Classes in Java

Hi All...!

Today I'm going to show you how to correctly declare a Java "Class"In Java we have to write almost everything inside a Class. In this post I'm not going to teach you what is a Class. I will teach you that in future post.

How to Declare a Class


In Java source code the first statement must be the Package Statement and the second statement must be the Import Statement and then the Declaration of Class.(don't worry about what is package and what is import you will learn them soon for now just stick with the words).

Following code shows how the Package statement, Import statement must be written in Java source code and also how to declare a class.





Brief idea about package and import statement


Package is some thing like a folder. If you mention a package statement in your source code that means your source code file is inside that folder. That folder's name will be your package name.If we take above source code, file name is newpackage.

Import  means we are going to use some Java source codes which has already created by someone and get some help from that

For a example think you want to use a calendar in your Java code if you can find a suitable calendar build using Java from somewhere else you can import and use it in your code.

Declare The Java Class 


After we typed package and import statement, then we have to declare our Java class. As the code shown above we can create a java class.In more advanced programs we sometimes have to add more parts to that class declaration I will teach you guys about that in future for now this type of class declaration is enough to do our codes.

In above shown code we have create a class call "MyNewClass" with Default access modifier.(access modifiers are some Java keywords that controls the access levels to the source code. Don't worry stay cool I will teach you guys that also in future).

After the class name you can see there are two squiggly brackets they mark the scope of the class.As I told you above in Java we have to write everything in side the class scope that means we have to write everything in side those squiggly brackets.


Note

We don't want package and import statement always in our Java code.But if we are going to use those statement we must use them as shown in above code. 




No comments:

Post a Comment