Ordering Elements
Start course
2h 5m

In this course, we'll learn the object-oriented concept in Java.

Learning Objectives

  • Object-Oriented programming concept
  • Object & Class
  • Access Modifiers
  • Naming Conventions 
  • Constructors
  • Packages
  • Static Keyword
  • Nested and Inner Classes

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams


  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge

Hi there. In this lesson we'll talk about how to order elements in Java. So far, we've learned many concepts, such as package, method, variable, etc. So, can these elements be randomly ordered in Java?

Of course not. All of them must be created in a certain order and in a sequence. If you want, let's examine the table on this slide at this stage. If we look at the elements we use in Java, they are packages, import statements, classes, methods and variables. Let us now examine them in order. To give an example of packages, Scope or, are examples of a defining package. Of course, they should be used with the package keyword. So is it mandatory to have packages in Java? No. Where are they defined? The packages in Java should be defined at the very beginning. If you examine the examples we've made so far, you can see that the package names are always defined on the first line. Now let's look at the import statement. For example,, imports the date class, while java.util.* imports all classes in the util package. 

This must also be used with the import keyword and the import is also not required in Java. We should also use the import statement right after the package definition. There should not be any elements between the package in the import definition. Let's look at another element, class. If we give an example of defining classes, we can define them as scope example or calculator. We define it together with the class keyword. You can also use an access modifier before the class keyword. In addition, the scope of the class should be indicated with curly braces. There must be at least one class in Java, so the class is absolutely necessary for Java. We also define classes right after the import statement. Let's look at another element, methods. Methods are elements that have at least one return type and a name. For example, void myMethod() is a parameterless method that does not return a value, while public static int sum( int a, int b) is a two parameter method that returns a value. 

You can also use specifiers before the return type of the methods. Also, there should be a parentheses right after method names and methods should have curly braces that indicate its scope. The methods aren't required in Java. We can define methods anywhere within classes because methods are part of classes. Finally, let's look at the variables. You have already seen many examples of using variables. The examples in the table are some of these uses. The variables are also not required in Java. We can use variables anywhere inside classes or methods. Yes, this is the order of elements in Java. Now let's move on to eclipse and get some practice. First, let's create a class. I will right click on the scope package we created in the previous video. The class name can be; ordering elements, and click Finish. 

Okay. As you can see, we have a class named, ordering elements and the package name on the first line. First, let's cut the package and I will paste it just after the scope of the class. And as you can see, we get a compilation error. It says, 'syntax error on tokens, misplaced constructs'. Let's fix it. Now, I will import a class from the java.util package. If I write, import just above the package. As you can see, we get a compilation error again because the import expression must come after the package definition. Let's move the import expression just after the scope of the class. As you can see, the compilation error occurred again. So in the java file, first, we should define the package and after the package, import expression, and after the import, the class definition should come. Also, we can create different classes in the same java file. So here, I write, class scope {}, but you cannot create another public class. For example, let's create, public class calculator{}. 

As you can see, we get a compilation error. It says, 'the public type calculator must be defined in its own file'. I will convert it to the comment line. And in addition, we cannot create a class that has a private or protected modifier. For example, I will create the private class calculator{}. As you can see, we get a compilation error again. It says, 'illegal modifier for the class calculator, only public, abstract and final are permitted. So it allows only the public, abstract and final modifiers. We will learn the abstract and final key words in the next lectures. Okay, now let's create a method. We can create the methods only in the scope of the class. For example, we cannot create any method outside the class scope. As you can see, the compilation error occurs, but in the scope of the class we can create a method; public void sum(). Also, we can create more than one method in the class. 

Let's create another method; int multiply(). In each of return in int value I write, return 5. Let's create the main method. If you write main here and click the main option, the main method will be created automatically. Or you can write it yourself. As you can see, there are three methods in the same class. If I want, I can create more methods here. The other point you should pay attention to is you cannot create methods in each other. For example, let's cut this multiply method and paste it to the main method. As you can see, we get a compilation error. It says, 'multiply cannot be resolved to a type'. As a result, we can create a method within the scope of the class, but we can call a method with its name within another method. 

For example, we can call the sum method inside the multiply method. Also, if we use the static keyword for the sum method, we can also call it in the main method again. Okay, last, let's look at the variables. We can define the variables inside the classes or methods. For example, in the class I write, int age=20. Now let's create another variable in the sum method; Char myChar ='a'. As you can see, we create a variable both inside the class and methods. Yes, this is the order of the elements in Java. To summarize, first, we define the package, and after the package definition, we define the import. After the import, we define the classes, and inside classes, we define the methods. And the variables can be defined in the class and methods. Let's take a short break here. See you in the next lesson.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics