Tuesday, July 14, 2015

static

- I dont need to create object to access the static methods and variables. If method or variable is made is static, then it will not have copies created through objects.

- Behaviour is Singleton(a single person or thing of the kind under consideration). Having one object at one point of time.

- If method is made static then it is indirectly saving memory. As it will have only one object, so no more instances, so no more memory is needed.



Static Import:


12/22/2015

We invoked class Math's static fields and methods by preceding each with the class name Math and a dot (.). A static import declaration (a new feature of J2SE 5.0) enables programmers to refer to imported static members as if they were declared in the class that uses them—the class name and a dot (.) are not required to use an imported static member.
    A static import declaration has two forms—one that imports a particular static member (which is known as single static import) and one that imports all static members of a class (which is known as static import on demand). The following syntax imports a particular static member:
import static packageName.ClassName.staticMemberName;
where packageName is the package of the class (e.g., java.lang), ClassName is the name of the class (e.g., Math) and staticMemberName is the name of the static field or method (e.g., PI or abs). The following syntax imports all static members of a class:
import static packageName.ClassName.*;
where packageName is the package of the class (e.g., java.lang) and ClassName is the name of the class (e.g., Math). The asterisk (*) indicates that all static members of the specified class should be available for use in the class(es) declared in the file. Note that static import declarations import only static class members. Regular import statements should be used to specify the classes used in a program.


static and final together:

Source: http://www.coderanch.com/t/394580/java/java/Final-Static

final declares a variable to be constant. Which means once you set its value, it never changes and trying to change it gives an error. An example of using final is:
final int i = 0;
i is equal to 0, it will always be equal to 0 and can never be changed.

static is a completely different animal. static just means that this variable exists outside of any class. They are usually referenced via class name 'dot' variable name like so:
static stVar is declared in Class1. It is a member of that class but can be accessed even if no instances of Class1 exist. It can be accessed with Class1.stVar.
Hope this helps,


In addition to Jason's excellent post, here is some more information about final and static.
The keyword final is used to prevent a method from being overridden, or to prevent a class from being inherited. It can also be used to create variables whose values can't be changed, as Jason points out. 
The static keyword can be used to create a variable that is shared by all instances of its class. Thus, there is only one copy of a static variable that will be used by all objects of the class. As Jason points out, it can be accessed independently of any object. In essence, a static variable is similar to a global variable in other computer languages.
You can also create static methods. A static method can call only other static methods, access only the static data defined by its class, and not use this or super

9 comments:

  1. How do you call static methods and variables?

    ReplyDelete
    Replies
    1. The static member is callable on a class even when no instance of the class has been created. The static member is always accessed by the class name, not the instance name. Only one copy of a static member exists, regardless of how many instances of the class are created.

      .,
      .

      Delete
  2. import static java.lang.System;

    what does static do here?

    ReplyDelete
    Replies
    1. here this will let your class import static methods from the System package/class.

      Static import concept is covered in this blog.

      Delete
  3. Replies
    1. http://stackoverflow.com/questions/7486012/static-classes-in-java

      Delete
  4. I think we need to try static keyword on variables, methods, class. Can you let me know how it differs on each of these?

    ReplyDelete
    Replies
    1. - Static ; ---- This variables are class variables. Can be called in main method using class name. static method or a variable is not attached to a particular object, but rather to the class as a whole. They are allocated when the class is loaded. Remember, each time you call the instance the new value of the variable is provided to you.

      - static ; ---- main() method is one of the static method also called class method. Can be called inside main method using class name. As mentioned in static variable, static method is tagged to class.

      - static ; ----- The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. Not directly to the class.

      Delete