Understanding Static in JAVA

Normally, a class member (instance variables or methods) is accessed only through an object of its class. However, it is possible to create a member that can be used by itself without reference to a specific instance.

To create such a member, we precede its declaration with static keyword.

Any static member can be accessed before any object of its class are created or even exist.In simple language you can understand that static members don’t care of instantiation (object creation) of its class. They are always available.

Both variables and methods can be static.

The most common example is main() method. It is declared as static because it is called before any object exists. static allows main() to be called without having to instantiate a particular object of class. This is necessary since main() is called by JVM before any objects are made.

When objects of such a class, in which instance variables are declared as static, are created, no copy of static variable is made. That static variable exists singly- the original. Let me show this with couple of codes (first code is the normal program without static and second code is with static) –

Program-1
this is the normal program without static keyword
OUTPUT- Tony Stark
Tony Stark
Tony Stark
Dwayne Johnson

Above code and its output should be quite clear. I have created two objects (h1, h2) of class ‘Heroes’ and first printed the ‘name’ of both these objects. Look they give same output because I have initially initialized the ”name” with ‘Tony Stark’. Don’t get confused that only one ‘name’ is shared by both objects. This is not the case, both have separate memories for their particular ‘name’. Then I have changed the ‘name’ attribute of h1 and then again printed the ‘name’ of both the objects. In this case the name attribute of h2 remained same (‘Tony Stark’) while that of h1 changed to ‘Dwayne Johnson’. This clearly shows that both the objects do have separate memory allocation for ‘name’. This means two copies of ‘name’ are created – one for h1 and other for h2. Right !

Now look carefully the following code where I have made just a small change- ‘String name’ to ‘static String name’ in second line.

Program-2
OUTPUT – Tony Stark
Tony Stark
Dwayne Johnson
Dwayne Johnson

In above code, both objects (h1 and h2) share same memory so if changes are made in h1, it is clearly reflected in h2. I hope you got it… Thus all instances of the class share the same static variable.

Methods declared as static have following restrictions:

  • They can only directly call other static methods.
  • They can only directly access static data.
  • They cannot refer to this or super.

Let me give you an example –

Program-3

In above example, one instance variable – ‘percent’ is static and print() method is also static so they can be directly accessed in any static method. Since main() is static so look how it directly accesses other static members (percent and print()) of class.

We can use static block also. It is actually used to initialize static members of class. And important thing to note is it is executed before main(), so yes if you are thinking to run your program without main() it will run if you have static block. That’s why this concept is invalid in current versions of JDK.

It is not at all useful in current versions of Java. So let’s focus only on important things. ๐Ÿ™‚



How to access static members outside the class ?

Not much tiring, especially when you are dealing with static members. Static members are accessed by the use of the dot operator on the name of class. In Program-2 I have accessed static members through objects but let me tell you this is not at all needed, this is wasteful. You can access them directly. I showed that to you just to make the difference clear between static variables and non-static variables.

Now look at this code, I even don’t need to create objects to access them, however we can create when needed.

Program-3
OUTPUT – Tony Stark
Dwayne Johnson

That’s it for static…

Have a good time !

Display Data from Database in Web Page

In our previous post, we successfully created a table and inserted five records in it. Now we will display the same data in a web page through a PHP file. There are some functions used to do this and this is an important part to be known. And these functions which we are going to use will be common in all php files where you intend to display something on your web page from the database.

We created this table shown below-

and now, from the above table, if we want to display the name of all books in a web page, then following piece of code will be used.

For the above code I am using PHP designer+WAMP server. Let me now explain the code.

  • mysqli_connect(hostname, username, password, database name, port, socket) ‘ function opens a new connection to the MySQL server. It has six optional parameters. I have used only first four of these. ‘myproject’ is the name of my own project you write yours and there is no password for my database so I have left it blank “”.
  • In line 7 I have stored my query in a variable $query. Please note that we pass queries in database, we cannot pass the queries elsewhere in the same manner as we do in database and since this is a PHP file, we are storing the query as a string in variable $query.
  • In line 9, the function mysqli_query() function performs the query (that we stored in $query) in the database(we have sought the connection and stored it in $link) and so we have passed $link and $query inside it. This returns an object. You can view its return type through var_dump(mysqli_query($link, $query));
  • In line 11 mysqli_fetch_row() function returns a row from the $resultset. Please note that $resultset variable holds all the records of the table but it is not in the proper format to be displayed. That’s why we use this function- to actually pick data from the table and display it in the way/format we want.
  • And the row extracted from the table through mysqli_fetch_row is stored in $r. That means $r is now an array. And so we can display whatever data we want with the use of indices.
  • I have displayed $r[0], because in the table the name of book is stored in first column of each row that is at index 0 of each row. I hope you got this explanation and if not then no issues simply write your doubt in comments.

With this you can display the name of books stored in your database in a web page. But remember this is not at all a proper way to do. We must have seen in various sites that data is presented in a proper way mostly in table form.

We’ll also display this data in a proper table format in our next post. Till then keep coding and keep consuming coffee cups :).

Have ERROR FREE coding!