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 flowkl;
  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).



ReactJS with Redux Online Training by Edureka

About Harish Kumar

Harish, a technical core team member at www.lyflink.com with five year experience in full stack web and mobile development, spends most of his time on coding, reading, analysing and curiously following businesses environments. He is a non-graduate alumni from IIT Roorkee, Computer Science and frequently writes on both technical and business topics.

Related Articles

PHP stands for PHP: Hypertext Preprocessor. PHP is known as a server side scripting and interpreter language for web ...
Introduction to PHP
In this tutorial, we will study about conditional statements (if else statements and switch statements). These statem...
Conditional statements in php
In this tutorial, we will study about strings and operations upon strings. Due to long length of this section, I have...
Strings in PHP (part-1)

Complete Python Bootcamp: Go from zero to hero in Python 3

Top Posts

Recent Posts

The Complete Web Developer Course - Build 25 Websites

Meet on Twitter

Subscribe

Subscribe now and get weekly updates directly in your inbox!

Any Course on Udemy in $6 (INR 455)

Development Category (English)300x250

Best Udemy Courses

PHP with Laravel for beginners - Become a Master in Laravel