Important Keywords in Java

Public, Private and Protected:

Access specifier tells where that instance/ object/ method/ variable/ class can be accessible. public means method is open for all and it is accessible outside the class and even package. private means that instance is accessible only inside the class but will not be called outside from the class. This is for security issues. protected means that instance can be called from inside the class as well as from the class which is inheriting from this class ( inheritance is concept of OOP and we will read it in some other tutorial).

Static and Dynamic:

All methods/ objects/ variables/ instances can be of two types, static and dynamic. There are two major differences between these two terms:

  1. Static instances are independent from all objects of the class and can be called directly using class name whether we have to make object for dynamic instances.
  2. Static instances has its own fixed predefined memory but dynamic instances are assign new memory only when they are called. It saves memory because these occupy memory only during running time and after that memory becomes empty.

In this example, variable 'a' of class Check is static and variable 'b' is dynamic. In main function we are making two objects of class Check and are calling method test, which is increasing value of 'a' and 'b', 3 times for each object.

NOTE: Don't try to understand all concepts of the code, just look only on static and dynamic concept. We will read about other concepts later.

  1. package findalltogether;
  2. class Check
  3. {
  4. static int a = 2;
  5. int b = 3;
  6. public void test()
  7. {
  8. a++;
  9. b++;
  10. System.out.printf("a is %d and b is %d \n",a,b);
  11. }
  12. }
  13. public class Tutorials
  14. {
  15. public static void main(String args[])
  16. {
  17. Check first = new Check();
  18. Check second = new Check();
  19. System.out.println("for object first:");
  20. first.test();
  21. first.test();
  22. first.test();
  23. System.out.println("for object second");
  24. second.test();
  25. second.test();
  26. second.test();
  27. }
  28. }

Output:

  1. for object first:
  2. a is 3 and b is 4
  3. a is 4 and b is 5
  4. a is 5 and b is 6
  5. for object second
  6. a is 6 and b is 4
  7. a is 7 and b is 5
  8. a is 8 and b is 6

You can see that static variable is behaving same for static variable but different for dynamic variable. It is because static variable has fix memory location and doesn't allocated new memory for each new object as it is independent from all class objects while dynamic variable is assigning new location for each object.

Arguments vs Parameters:

Argument is the extra information which we will pass to a method. Parameter is the extra information that a method requires. The key difference is that arguments are passed during the execution and after passing they are parameters for that method.

Global variables vs Local variables:

The variables which are defined inside the class are called global variables and also known as class variables. Other variables which are defined inside any method/ conditional statement/ loop are called local variable of that method/ conditional statement/ loop.

Global variable are accessible everywhere inside the class except dynamic variables which are not accessible in static methods. Local variable are only accessible in their local environment (method/ conditional statement/ loop).
[000webhost_bottom_banner]



About Harish Kumar

Harish is an interested person in the field of web development and blogging. He works for the need of young web developers in learning various languages, latest technologies and other essential tips and tricks. If you need some help or you have some suggestion then you email him at harish@findalltogether.com without any hesitation. You can also suggest/demand for articles of your own choice.

Related Articles

Java is object oriented and platform independent programming language which runs on JVM. Lets see advantages of java ...
Introduction to Java
In this tutorial, we will learn how to give/ take input/ output to console. What are classes, main() and comments? Wh...
First program in Java
Variable is a memory location which stores data. In Java, we have to define what type of variable (integer, text, str...
Variables and data types in Java

Login or Sign up to leave comment.