This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java" Watch "java" New topic
Author

java

sac abc
Greenhorn

Joined: Sep 01, 2006
Posts: 1
how to use this and super keyword
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
Please don't use such vague titles for your questions: read this.

how to use this and super keyword
Practice, practice, practice.


The "this" keyword refers to the object you happen to be using at the time. It cannot be used in a static method.
  • You can use it at the beginning of an overloaded constructor. this(foo, 1); passes control from one constructor to another which has (foo, int) as its parameters.
  • It can refer to the whole object. A call like "MySerializingClass.serialize(this, myFile);" serializes the entire object into the file "myFile."
  • It can be used before the name of an instance field or instance method to distinguish them from local names. In a method with "bah" in its parameters or local variables, "this.bah" means the "bah" which is a field, not the "bah" which is a local variable.

  • The "super" keyword refers to that part of the object which is also in its superclass.
  • You have to use it as the first line in at least one constructor unless the superclass has a public no-argument constructor; "super(foo);" calls the constructor in the superclass which has "foo" as its parameters.
  • You can call "super.bah()" from anywhere inside any instance method if the "bah" method has been overridden.
  • If you can do this there has been some bad design, but if there is a field which is shadowed by a field in the subclass with the same name and you have access to it (!), you can call it with "super.baah" rather than "baah."
  • The last example is very bad design (no encapsulation, two variables with the same name), so never use it.

    If any more details are required, you will have to go through the books.
    Srinivas Kalvala
    Ranch Hand

    Joined: Oct 20, 2005
    Posts: 257

    Hello,

    Super can be used in following ways,

    one is to call the super() constructor.

    second is to call the super. method and another one is to
    super.attribute assignemnt.

    and explore about "this".... all the best
    Sanjaya Sugiarto
    Ranch Hand

    Joined: Mar 25, 2004
    Posts: 229
    this: executing actual object. It's like 'me' in VisualBasic.
    super: you call the parents object.

    Used in inheritent cases (interfaces, abstracts, extends, etc).





    If you do new HelloWorld2() than the output would be: hi hello in HelloWorld1. If you do new HelloWorld2("hi ", "ok i got this") than the first output would be: hi hello in HelloWorld2.
    Notice I used this() and super() --> only for constructors.
    There are also this.<methodsname> and super.<methodsname> (with dot) --> for all others except constructors. The principle is the same though.


    <a href="http://www.wi.hs-furtwangen.de" target="_blank" rel="nofollow">Business Information Technology - Hochschule Furtwangen University, Germany</a>
    Jay Suttiruttana
    Ranch Hand

    Joined: Aug 17, 2006
    Posts: 36
    "this" can identify the member of the class with relation to its variable/attribute.

    ex:

     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: java