Showing posts with label How. Show all posts
Showing posts with label How. Show all posts

Monday, 31 March 2014

Static class in Java

Static class in Java

Can a class be static in Java?

The answer is YES, we can have static class in java. In java, we have static instance variables as well as static methods and also static block. Classes can also be made static in Java.

Java allows us to define a class within another class. Such a class is called a nested class. The class which enclosed nested class is known as Outer class. In java, we can’t make Top level class static. Only nested classes can be static.

Nested Classes

The Java programming language allows you to define a class within another class. Such a class is called a nested class and is illustrated here:
class OuterClass {
...
     class NestedClass {
        ...
    }
}

Nested classes are divided into two categories: static and non-static. Nested classes that are declared  
What are the differences between static and non-static nested classes? 

static are called static nested classes. Non-static nested classes are called inner classes.
1) Nested static class doesn't need reference of Outer class but non static nested class or Inner class requires Outer class reference. You can not create instance of Inner class without creating instance of Outer class. This is by far most important thing to consider while making a nested class static or non static.
2) static class is actually static member of class and can be used in static context e.g. static method or static block of Outer class.
3) Another difference between static and non static nested class is that you can not access non static members e.g. method and field into nested static class directly. If you do you will get error like "non static member can not be used in static context". While Inner class can access both static and non static member of Outer class.

Static Nested Classes

As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class: it can use them only through an object reference.

Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.



Inner Classes

As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.


here is the code sample of using both nested static class vs non static class :

/*
* Java program to demonstrate What is nested static and non static class.
* How to create instance of static and non static class and How to call
* methods of nested static and Inner class in Java. Overall comparison of
* static vs non static class.
*/
class Outer{
private static String message = "HelloWorld";

// Static nested class
private static class MessagePrinter{

//Only static member of Outer class is directly accessible 
//in nested static class

    public void printMessage(){
     // Compile time error if message field is not static
        System.out.println("Message from nested static class : " + message);
    }
}
//non static nested class - also called Inner class
private class Inner{
    // Both static and non static member of Outer class 

    //is accessible in this Inner class
    public void display(){
        System.out.println(" Message from non static nested or Inner class : " + message);
    }
}
// How to create instance of static and non static nested class
public static void main(String... args){
    // creating instance of nested Static class
    Outer.MessagePrinter printer = new Outer.MessagePrinter();
    //calling non static method of nested static class
    printer.printMessage();
    // creating instance of non static nested class or Inner class
    // In order to create instance of Inner class you need an 

    //Outer class instance 

    Outer outer = new Outer(); 

    //outer class instance for creating non static nested class

    Outer.Inner inner  = outer.new Inner();
    //calling non static method of Inner class


    inner.display();
    // we can also combine above steps in one step to 

    // create instance of Inner class

    Outer.Inner nonStaticIner = new Outer().new Inner();
    // similarly you can now call Inner class method
    nonStaticIner.display();
}

}

Output:
Message from nested static class : HelloWorld
Message from non static nested or Inner class : HelloWorld
Message from non static nested or Inner class : HelloWorld


Why Use Nested Classes?

Compelling reasons for using nested classes include the following:
  • It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.
  • It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.
  • It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.

Sunday, 9 February 2014

Starting with jQuery (First program in jQuery)

jQuery Installation:


Adding jQuery to Your Web Pages

There are several ways to start using jQuery on your web site. You can:
       Download the jQuery library from jQuery.com
       Include jQuery from a CDN, like Google

Downloading jQuery

There are two versions of jQuery available for downloading:
       Production version
       Development version

Steps needs to follow:

1.    Open your favorite browser and point it to this address: http://www.jquery.com
2.    Find the section labeled “Grab the Latest Version!” Then, select the checkbox next to “Production.”.
3.    Click the “Download jQuery” button.
4.    The next page you’ll see will look something like this. Save the page into a folder called scripts on your drive



See figure:

What’s the difference between the Production and Development versions?

       The Production version of jQuery is a minified version, intended for speed of execution on a web server.
       The Development version is intended for developers interested in exploring and extending the inner workings of thejQuery library.



How to use downloaded jQuery library?

The jQuery library is a single JavaScript file, and you reference it with the HTML <script> tag (notice that the <script> tag should be inside the <head> section):
<head>
<script src="jquery-1.10.2.min.js"></script>
</head>


Do you wonder why we do not have type="text/javascript" inside the <script> tag?

This is not required in HTML5. JavaScript is the default scripting language in HTML5 and in all modern browsers!

Alternatives to Downloading

If you don't want to download and host jQuery, you can include it from a CDN (Content Delivery Network). Both Google and Microsoft host jQuery.
To use jQuery from Google or Microsoft, use one of the following:

Google CDN:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
</head>

Microsoft CDN:

<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.2.min.js">
</script>
</head>

One big advantage of using the hosted jQuery from Google or Microsoft
Many users already have downloaded jQuery from Google or Microsoft when visiting another site. As a result, it will be loaded from cache when they visit your site, which leads to faster loading time. Also, most CDN's will make sure that once a user requests a file from it, it will be served from the server closest to them, which also leads to faster loading time.


Starting with jQuery (First program in jQuery):

Open your editor -> Create a new document ->Paste given code -> Save as MyFirstPage.html -> Open that page in Browser
*Note:- While saving select “save as type” as all type(“.”)

MyFirstPage.html
<!DOCTYPEhtml>
<html>
<head>
<title>jQuery goes to DOM-ville</title>
<style>
#change_me {
       position: absolute;
       top: 100px;
       left: 400px;
       font: 24px arial;
}

#move_up#move_down#color#disappear {
       padding: 5px;
}
</style>
<script src="jquery.js" type="text/javascript">
      
</script>
</head>
<body>
       <button id="move_up">Move Up</button>
       <button id="move_down">Move Down</button>
       <button id="color">Change Color</button>
       <button id="disappear">Disappear/Re-appear</button>
       <div id="change_me">Make Me Do Stuff!</div>
       <script>
              $(document).ready(function() {
                     $("#move_up").click(function() {
                           $("#change_me").animate({
                                  top : 30
                           }, 200);
                     });//end move_up
                     $("#move_down").click(function() {
                           $("#change_me").animate({
                                  top : 500
                           }, 2000);
                     });//end move_down
                     $("#color").click(function() {
                           $("#change_me").css("color", "purple");
                     });//end color
                     $("#disappear").click(function() {
                           $("#change_me").toggle("slow");
                     });//end disappear
              });//end doc ready
       </script>
</body>
</html>

How does that work?

       The visitor clicks a button.
 The JavaScript interpreter “hears” the click event and runs the function attached to it.
The JavaScript interpreter changes the DOM representation of the page.

 The visitor sees the element move up the page.