What does the this keyword do?

i don't really understand this code very well, can anyone give me a link or a little explanation that would help me with it? I'm not sure what the this keyword does, and the toString method is puzzling as well.
thanks.
public class Friend
     protected String name;
     protected String telephone;
     protected Friend next;
     static Friend list;
     public static void print() {
     Friend friend = list;
     if(friend == null) System.out.println("The list is empty.");
     else do {
     System.out.println(friend);
     friend = friend.next;
     } while (friend != null);
     public Friend(String name, String telephone)
     this.name = name;
     this.telephone = telephone;
     this.next = list;
     list = this;
     public String toString()
     return new String(name+":\t"+telephone);
class TestFriend
     public static void main(String args[])
     Friend.print();
     new Friend("Anita Murray", "388-1095");
     new Friend("Bill Ross", "283-9104");
     new Friend("Nat Withers", "217-5912");
     Friend.print();
}

about the code in post 1, is there any other code
that could replace what's inside the friend method
but would still print the same output?
for example could i replace "this.name = name;" etc
with anything else?In the context of that constructor, there are two name variables--the parameter and the member variable. The parameter is what was passed into the c'tor, and the member varaible is part of the state of the current object--the object being initialized.
So, when we just say "name", it means the parameter, and if we want to refer to the name variable that's part of the object, then we say "this.name"--that is, the name varaible belonging to "this" object (the "current" object).
If there were no local variable or parameter called "name", then instead of "this.name" we could just say "name" to refer to the current object's name variable, because there would be no ambiguity.

Similar Messages

  • What does the private keyword mean exactly?

    Hi,
    I thought I understood what "private" meant, but apparently not fully.
    Can someone explain the difference between the following two chunks of code and why one compiles when the other doesn't?
    class Component
      private Object loopID;
      private Container container;
      public boolean isActive()
        return loopID == container.loopID; //Doesn't Compile
        return loopID == getContainer().loopID; //Does compile
      public Container getContainer()
        return container;
    class Container extends Component
    }

    Put simply, private means you can't access the
    variable from outside
    the class. So you cannot use the "." operator to get
    access to it.
    But the method can return a reference to it, so the
    accessor method
    allows you to return a reference to the variable.
    Think of it this way:
    If you want to do "x.y" or "x.y()", the
    variable/method has to be public.
    If the variable/method is private, you can't access
    it with ".".
    By the way, there is an edit button--It is in the
    right corner above the message. I just added this
    with it. I didn't know it was there before.
    Message was edited by:
    WhyNotFerzlePut simply: no. Private means that access is only allowed from within the body of the top level class where it the member or constructor is declared. Your example is just wrong on a number of levels. I can access the member of an instance using "." if I have access, and I cannot if I do not have access.

  • EJB QL-What does the Object keyword in select clause do ?

    I always wondered when do you need to use the Select object( ) keyword ? I read this:
    [http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/EJBQL5.html#73030]
    but didn't find it very clear.
    Cheers,
    Michal

    see
    @ejbgen:entity Annotation
    go to
    http://edocs.bea.com/workshop/docs81/doc/en/core/index.html
    see
    max-beans-in-cache
    Set it to 1
    See also
    @ejbgen:finder Annotation
    - group-name
    - max-elements
    set it to 1 or to desired
    optional:
    Add to a group the desired cmp fields, if u don't want to display all the columns.
    After the exec. of the findMethod u should have 1 Object, one EJB inst., in your Collection, and furthermore only the desired DB table columns

  • The this keyword

    I have a vague idea what the this keyword means. Its something to do with inheritance but I'm not sure how this works. For example I've just seen a program as shown below:
    import java.awt.*;
    import java.util.*;
    import java.applet.*;
    import java.text.*;
    import javax.swing.*;
    public class Clock extends JApplet
    implements Runnable {
    private volatile Thread clockThread = null;
    DateFormat formatter; //Formats the date displayed
    String lastdate; //String to hold date displayed
    Date currentDate; //Used to get date to display
    Color numberColor; //Color of numbers
    Font clockFaceFont;
    Locale locale;
    public void init() {
    setBackground(Color.white);
    numberColor = Color.red;
    locale = Locale.getDefault();
    formatter =
    DateFormat.getDateTimeInstance(DateFormat.FULL,
    DateFormat.MEDIUM, locale);
    currentDate = new Date();
    lastdate = formatter.format(currentDate);
    clockFaceFont = new Font("Sans-Serif",
    Font.PLAIN, 14);
    resize(275, 25);
    public void start() {
    if (clockThread == null) {
    clockThread = new Thread(this, "Clock");
    clockThread.start();
    public void run() {
    Thread myThread = Thread.currentThread();
    while (clockThread == myThread) {
    repaint();
    try {
    Thread.sleep(1000);
    } catch (InterruptedException e){ }
    public void paint(Graphics g) {
    String today;
    currentDate = new Date();
    formatter =
    DateFormat.getDateTimeInstance(DateFormat.FULL,
    DateFormat.MEDIUM, locale);
    today = formatter.format(currentDate);
    g.setFont(clockFaceFont);
    //Erase and redraw
    g.setColor(getBackground());
    g.drawString(lastdate, 0, 12);                     
    g.setColor(numberColor);
    g.drawString(today, 0, 12);
    lastdate = today;
    currentDate = null;
    public void stop() {
    clockThread = null;
    Now the line : clockThread = new Thread(this, "Clock");
    What does the this keyword do here?

    Please use code tags as described in Formatting tips
    Classes and Inheritance: Using the this Keyword

  • I was playing with my ipad settings (it's an older model) and noted in the advanced settings of Safari there was a place to view website databases.  When I clicked on this I saw websites.  How do these get there and what does the space amount mean?

    I was playing with my ipad settings and noted in he advanced settings of Safari there was a place to view "website databases".  When I selected this I saw a multitude of websites.
    Can anyone tell me how these get there?  Can a website be posted even if it was never went to?  What does the space amount mean?  For example, 1.5 kb...is this quite a bit?  Would it indicate someone has gone to a site multiple times?
    I share my ipad with my teenage daughter and I'm trying to find out if she's lying to me.  Obviously she's swearing that she has "no idea" how these got there and I'm trying to keep her safe (she's only 14).
    Thanks everyone.
    Concerned Mom

    Think of your PC and the 'temporary internet folder' where it keeps cached copies of web pages or elements off a web page for 'quicker display the next time you visit'. That's pretty much what that folder is. 1.5K is tiny. Probably just a basic page with some text on it. (you might be confusing 1.5K with 1.5 megabyte....megabyte is large...it's roughly 1000 kilobytes, so the 1.5K is a tiny file)
    As far as I know, the only way info gets into that folder is if the browser has been to that site.
    if you have a concern there are browsers out there, McGruff is one i've seen recommended, that allow some degree of parental control and supervision. That or you could passcode lock the iPad or enable the restrictions to turn off some parts of the device to have some control.

  • I have a wireless network at home. I run my iphone and ipad on it with no problems. Just purchased a MacBook Pro, and now my wi-fi keeps disconnecting and i get an error message saying another device is using the same IP address. What does all this mean?

    I have a wireless network at home. I run my iphone and ipad on it with no problems. Just purchased a MacBook Pro, and now my wi-fi keeps disconnecting and i get an error message saying another device is using the same IP address. What does all this mean?

    Hello AKCamus
    To give you some ideas of what to do next, I have provided a few articles that will give some troubleshooting Wi-Fi connections and recommended Wi-Fi settings.
    iOS and OS X: Recommended settings for Wi-Fi routers and access points
    http://support.apple.com/kb/HT4199
    iOS: Troubleshooting Wi-Fi networks and connections
    http://support.apple.com/kb/ts1398
    Troubleshooting Wi-Fi issues in OS X Lion and Mac OS X v10.6
    http://support.apple.com/kb/HT4628
    Thanks for using Apple Support Communities.
    Regards,
    -Norm G.

  • Does anyone know what does the keyword section do?

    Does anyone know what does the keyword section do? Under "Any of the webpages -> Properties -> Basic tab -> Tags/Keywords"?
    Is it used for part of the google search?

    Its tagging (Classification), one of the bright feature of any content management system. It allows author to classify the content in system. There are various advantages but i would refer you to go through http://dev.day.com/docs/en/cq/current/wcm/tag_admin.html. Apart from that you can search in google as well to find relative information about classification in content management system.

  • This is probably a stupid question, but what does the red circle with a number in it mean on iCal? It looks just like the emails notification. I do not have anything do or up in my cal.

    This is probably a stupid question, but what does the red circle with a number in it mean on iCal? It looks just like the emails notification. I do not have anything do or up in my cal.

    Actually, another person on the "more like this" had the right answer. It meant I had a notification for an invite that I needed to respond to.
    Hope this helps you as much as it helped me.
    D

  • HT2433 what does the error message that says " You are logged out because another computer is using this IP address." mean?

    what does the error message that says " You are logged out because another computer is using this IP address." mean?

    Some other piece of network ghardware on your home LAN (Local Area Network) is using the IP address your Mac is trying to use.
    Reboot both your modem and router, or the Modem/Router combo box you have, and all pieces of networking equipment, computers and printers, you have connected to your LAN.
    If you continue to get that error then it is a bug in OS X or you have your Mac set to a Static IP address that is being handed out by the routers DHCP server to some other device.

  • What does the ~ symbol mean next to an event?  I have never seen this before but today when I edited a few events this appeared next to the event.

    What does the ~ symbol mean next to an event?  I have never seen this before but today when I edited a few events this appeared next to the event.

    Greetings,
    Hm. I've never seen that in iCal.  In some other Applications the tilde "~" indicates a restore point or a before / after for a file.  My guess is there is a broken cache file in iCal.
    Try:
    Go to Apple Menu > System Preferences > Language & Text > Formats > Region. Region should be set to the country of your choice. Do not have it set to "Custom" as this can have unexpected results in the iCal window.
    If that doesn't resolve the issue:
    1. First make an iCal backup:  Click on each calendar on the left hand side of iCal one at a time highlighting it's name and then going to File Export > Export and saving the resulting calendar file to a logical location for safekeeping.
    2. Go to iCal > Quit iCal
    3. Remove the following to the trash and restart your computer:
    Home > Library > Caches > com.apple.ical
    Home > Library > Calendars > Calendar Cache, Cache, Cache 1, 2, 3, etc. (Do not remove Sync Cache or Theme Cache if present)
    --- NOTE: To get to "Home > Library" in Lion: Click anywhere on the desktop and then click on the "Go" menu at the top of the computer screen while holding down the "option" key on the keyboard.  You will see "Library" in the menu.
    4. Launch iCal and test.
    If the issue persists:
    1. Go to iCal > Quit iCal
    2. Remove the following to the trash and restart your computer:
    Home > Library > Caches > com.apple.ical
    Home > Library > Calendars > Calendar Cache, Cache, Cache 1, 2, 3, etc. (Do not remove Sync Cache or Theme Cache if present)
    Home > Library > Preferences > com.apple.ical (There may be more than one of these. Remove them all.)
    --- NOTE: Removing these files may remove any shared (CalDAV) calendars you may have access to. You will have to re-add those calendars to iCal > Preferences > Accounts.
    3. Launch iCal and test.
    Hope that helps.

  • What does the class CL_EXITHANDLER do ? What the significance of this class

    what does the class CL_EXITHANDLER do ? What the significance of this class,

    Peters,
    Welcome to SDN!
    you can see this class in SE24 and than choose GET INSTENCE method DB on it. than put break-point on
    CALL METHODcl_exithandler=>get_class_name_by_interface
    finally run any t-code you will come into debugger mode and if you see in exit_name value after pressing F6 you will get BADi name of this perticuler T-code.
    basically we use this class for findings BADi.
    Amit.

  • I keep getting notices to install Adobe Flash Player version 10, however when I click on the various boxes, it then tells me that Flash Player does not support Vista. What does all this mean?

    I keep getting notices to install Adobe Flash Player Version 10, however when I click on the various boxes, it then says that Flash Player does not support Vista. What does all this mean and what is a Flash Player?

    I keep getting notices to install Adobe Flash Player Version 10, however when I click on the various boxes, it then says that Flash Player does not support Vista. What does all this mean and what is a Flash Player?

  • What does the PopUp "Prevent this page from creating additional dialogs" really mean and if I check the Box, how do I undo this if this was the Wrong Thing To Do?

    What does the PopUp "Prevent this page from creating additional dialogs" really mean and if I check the Box, how do I undo this if this was the Wrong Thing To Do?
    Details
    Constantly, while using Firefox (Beta v 11.0), and/or ESPECIALLY, when I have my eMail @ Mail.com open, I get this PopUp stating: "For security reasons, restarting mail.com Mail is possible only via the mail.com Mail Service. Please close this window and restart mail.com Mail" BUT Then there is a CHECKBOX: "Prevent this page from creating additional dialogs."
    I have no idea if Checking this Box is a Good Idea or if it will Screw-Up my mail.com Mail Service...
    Anyone have an answer or explanation of the Use of this Little Check-Box?
    BJ Orden [email protected]

    You do not get any pop-up windows on that website if you tick that box.
    That setting is stored in the cache and you should see pop-ups again if you reload the page and bypass the cache.
    Reload web page(s) and bypass the cache.
    *Press and hold Shift and left-click the Reload button.
    *Press "Ctrl + F5" or press "Ctrl + Shift + R" (Windows,Linux)
    *Press "Cmd + Shift + R" (MAC)

  • What does the faded music with a dotted circle beside it mean in the on this iPhone tab

    what does the faded music with a dotted circle beside it mean in the "on this iPhone" tab on itunes? Some even shw up twice, others once.

    Hi sjchr,
    Welcome to the Support Communities!
    The red square with a red circle around it is a symbol for a download in progress.
    Songs that have a cloud symbol beside them have been purchased, but don't reside on your device.
    When you click on the cloud icon, the song will be downloaded to the device, and you see a red square with a circle moving around.  If you wanted to stop the download, you would tap on the red square.
    Sometimes there may be an issue with the download, or the file may be corrupt.
    If the song is not playing correctly, the first thing I would suggest is to restart / reset your device:
    iOS: Turning off and on (restarting) and resetting
    http://support.apple.com/kb/HT1430
    If the issue persists, you may need to redownload the song. 
    Downloading past purchases from the App Store, iBookstore, and iTunes Store
    http://support.apple.com/kb/HT2519
    Cheers,
    Judy

  • What does the message (unable to launch app at this time try again lat ....done. Retry

    What does the message mean( unable to launch app at this time   Done.    Retry ) .

    The only other thing I can think of since this problem has to do with downloading on 3G is that the signal strength isn't good enough. Try a reset on the device, hold the sleep/wake and home buttons together until you see the Apple logo and release. Once the phone restarts see if you can get it done. If not, you might want to check with iTunes support and see if they see anything wrong with your account.
    Have you tried downloading on wifi? Does this work? Have you checked in iTunes for available downloads to try and get what you have cleared to start over?

Maybe you are looking for

  • Folder action to change the label of the folder

    Hi all I would like to use Automator/Folder Actions to set the Label of the folder to show that it contains a file that was created or modified as follows: Red if in the last three days, Orange if over three days but less than seven; and Yellow if ov

  • Indesign CC 9.1 Crash on Launch - Mountain Lion

    I get 2 errors messages : Unable to launch Adobe InDesign as you may lack sufficient permissions to access the preferences folders. Try deleting the preferences folders and then restart Adobe InDesign. Adobe InDesign is shutting down. A serious error

  • CSV File download error

    Hi, We have a struts wweb application deployed on weblogic 8.1.3. There is a csv file download functionality in one of the screen. On that screen, first user has to select certain filter parameters and depending on selections made, he gets a set of r

  • BPM certification questions

    Hi friends, I taking up XI Certification this week. Could anyone pls send me sample questions in BPM and other areas or XI. I am reading thru the certification material. If you can advice me tricks in exam it would be wonderful. Regards, Jeff

  • Maintenance Wizard install

    I would normally post this to the Maintenance Wizard forum, but it appears to still be inadvertantly archived. In the Maintenance Wizard Install Instructions (329476.1), Step 1. a. ii. h. says: Verify that $HOME does not have execute permissions for