Being specific in the Constructor, only when necessary

Hello! I am new to Java. I have a few really simple question. I know this much so far:
- Hiding is when a variable overrides another within a stronger (more inner) scope.
- Hiding is not good convention since it may confuse readers of code.
So I have this example. It's only part of an example, so it will only be the essential parts:
package example;
import java.awt.*;
public class Application {
     * The options component for this Application.
    Component optWindow;
    public Application() {
        Window optWindow = new OptionWindow();
        optWindow.pack();
        this.optWindow = optWindow;
    public void showOptions() {
        optWindow.setVisible(true);
    @Override protected void finalize() throws Throwable {
        optWindow.setVisible(false);
        optWindow = null;
        super.finalize();
class OptionWindow extends Frame {
    public OptionWindow() {
        super("Options:");
}Anyway, I also know that optWindow in the constructor hides this.optWindow. But I'm not sure 100% why.
My reasoning behind wanting to do this is that I don't think that the rest of the Application needs to know that optWindow is a Window other than in the constructor, so the Application Object should store it as just an abstract component. A sort of "type encapsulation and subclass separation".
I believe that the reason why this is hiding is because optWindow refers to the "reference" of the OptionWindow object, not the object itself =) I understand that so far.
But I believe it shouldn't because the this.optWindow does not refer to anything until after the constructor, so it shouldn't be hidden.
I have a few solutions I know of:
1) Drop the idea of storing optWindow as a Component in the Application Objects. Just store it as a window everywhere.
2) Keep it this way anyway.
3) Use a different variable name for the optWindow in the constructor.
There are in the order that I'd rather do them in. I understand that option 1 is the best. Option 2 might potentially be okay. Option 3, I really do not like since it's giving two names or references to the same object and it would be annoying to come up with a new name for it. But I could come up with something simple like "window" if necessary inside of the constructor.
My questions would be:
1) Is this a good idea to make the window an abstract component outside of the constructor?
2) Is this a "good" use of hiding; or does the rule always apply to avoid hiding?
If Question 1 is true, I'd probably go with Option 1. If Question 1 is false, but Question 2 is true, I'll go with Option 2. If Question 1 is still false, but so is Question 2, I'll go with Option 3.
Thank you very much in advanced! I really appreciate the help! =) And I look forward to asking more questions.
Edit: I wrote that OptionWindow throw Frame instead of "extends Frame".
Edited by: Macleod789 on Mar 30, 2010 2:24 PM

Macleod789 wrote:
Hello! I am new to Java. I have a few really simple question. I know this much so far:
- Hiding is when a variable overrides another within a stronger (more inner) scope.
- Hiding is not good convention since it may confuse readers of code.It's common and perfectly acceptable in some situations. Constructors and setters come to mind. If your code is well-written, it won't be at all confusing.
>
So I have this example. It's only part of an example, so it will only be the essential parts:
package example;
import java.awt.*;
public class Application {
* The options component for this Application.
Component optWindow;
public Application() {
Window optWindow = new OptionWindow();
optWindow.pack();
this.optWindow = optWindow;
public void showOptions() {
optWindow.setVisible(true);
@Override protected void finalize() throws Throwable {
optWindow.setVisible(false);
optWindow = null;
super.finalize();
class OptionWindow extends Frame {
public OptionWindow() {
super("Options:");
}Anyway, I also know that optWindow in the constructor hides this.optWindow. But I'm not sure 100% why.Because the JLS defines that it does. Local variables always hide members of the same name. That's the rule the designers decided on.
My reasoning behind wanting to do this is that I don't think that the rest of the Application needs to know that optWindow is a Window other than in the constructor, so the Application Object should store it as just an abstract component. A sort of "type encapsulation and subclass separation".\Huh? Reason behind doing what? Declaring the optWindow member as a Component rather than a Window? Well, yeah, if the rest of the Application class only cares that it's a Component and could do its job the same way if any other Component were used, then you're right--declaring it as a Component is the correct approach. But then it should not be named optWindow. And if the rest of the Application class doesn't need it to be a Window, then the c'tor has no reason to require that either.
I believe that the reason why this is hiding is because optWindow refers to the "reference" of the OptionWindow object, not the object itself =) I understand that so far.Reference vs. object is an important distinction to understand. However, it has nothing to do with hiding. Hiding is just about identifier names, like variables. The fact that their values are references is irrelevant to the hiding rules.
But I believe it shouldn't because the this.optWindow does not refer to anything until after the constructor, so it shouldn't be hidden.Show me the part of the JLS that says that hiding has do with whether the variable in question has been assigned or not.
In some scopes, two distinct identifiers with the same name can exist. There has to be some rule about which one takes precedence when there's no qualification. It's simpler if it's consistent and it's always the local. If it weren't, then a) it would be inconsistent, which would be confusing, and b) we'd need a new keyword or other mechanism to explicitly indicate that we're talking about the local. Or else we simply wouldn't be able to access the local variable in those contexts, which would be pretty stupid, I'm sure you agree.
I have a few solutions I know of:Solutions to what? What's the problem you're encountering?
1) Drop the idea of storing optWindow as a Component in the Application Objects. Just store it as a window everywhere.I addressed that above, and it has nothing to do with hiding.
2) Keep it this way anyway.So, a "solution" to whatever problem you're having is to do nothing and just keep having the "problem"?
1) Is this a good idea to make the window an abstract component outside of the constructor?If it's Component in the member variable, it should be in the c'tor too. If the c'tor requires a Window, then it must be because the member has to be a Window.
2) Is this a "good" use of hiding; or does the rule always apply to avoid hiding?It's very common and acceptable to have parameter names match member names where the parameter is being used to set the member.
Edited by: jverd on Mar 30, 2010 2:49 PM
Edited by: jverd on Mar 30, 2010 2:50 PM

Similar Messages

  • HI, I recently upgraded my operating system IOS 5.0.1 on an iphone 3gs. MY question is, how do I get the text messaging preview to just pop up so that I can see the name only when the lock screen is on? The conventional ways of doing this aren't working

    HI, I recently upgraded my operating system IOS 5.0.1 on an iphone 3gs. My question is, how do I get the text messaging preview to just pop up so that I can see the name only when the lock screen is on? The conventional ways of doing this, such as using turning off the SMS preview under the "passcode lock", well that option no longer exists, or if it does, I'm somehow missing it.
    When I go to the "messages" setting under settings on my phone, I still don't see an option of turning it off.
    If I can just get the name of the person sending the message, as I had it before, I'd be happy.
    Help!

    That option still exists in Settings > Notifications > Messages > Show Prview (OFF) and View in Lock Screen (ON). Here's how to configure your Notifications and Notification Center for iOS 5: http://iphone-and-i.blogspot.com/2012/01/how-to-customize-notification-center-in .html

  • My external HD of 1TB is not being recognized by the iMac and when after a long waiting period it does, the whole computer becomes extremely slow. Do I need any particular add-on to make it workable?

    My external HD of 1TB is not being recognized by the iMac and when after a long waiting period it does, the whole computer becomes extremely slow. Do I need any particular add-on to make it workable?
    Before I updated to OS X Mavericks I was using another external disk that was getting disconnected ocasionally so I bought a new ADATA HD710 portable 1TB Drive and moved the files to it. This new drive which is supposed to be OS X Mavericks compatible takes minutes to show the files especially when opening second and third level folders which has made mu iMac unusable
    What should I do?

    Your Mac should be able to recognize many different types of drives (including Windows-formatted drives) immediately for Reading, provided they are in good working condition and not damaged. There should be no need to transfer to a different type of drive.
    If there is no recognition of this drive, see if it can be seen in Disk Utility.
    If your aim is to transfer the data on it DO NOT use ERASE or PARTITION as these will completely erase all the information on the Drive.
    If the drive is present while the Mac sleeps, it is likely to drop out and not be seen any longer, especially if it is a USB-powered drive.

  • HT5463 Hi I am on an iphone 5 IOS7 and when I look at the 'Silence' setting at the bottom of the Don Not Disturb' screen - then even though all of the settings on that screen are off i.e. not green - the 'Silence' -'Only when iPhone is locked' setting is

    Hi I am on an iphone 5 IOS7 and when I look at the 'Silence' setting at the bottom of the Don Not Disturb' screen - then even though all of the settings on that screen are off i.e. not green - the 'Silence' -'Only when iPhone is locked' setting is tickedP

    Chris thanks for your reply, if none of the Do Not Disturb settings are switched on - then am I right that this setting will have no effect. I.e. I want my phone to ring at all times, so have turned off all of the do not disturb settings

  • I have the iPhone 4 recently became weak Wi-Fi where I can not use the Internet only when sitting Bejjani router, Can anyone help me in solving this problem please iPhone 4, iOS 7.0.3

    I have the iPhone 4 recently became weak Wi-Fi where I can not use the Internet only when sitting Bejjani router, Can anyone help me in solving this problem please
    iPhone 4, iOS 7.0.3

    There's a whole lot to read in your post, and frankly I have not read it all.
    Having said that, this troubleshooting guide should help:
    http://support.apple.com/kb/TS1538
    In particular, pay attention to the mobile device support sections near the bottom, assuming you have already done the items above it.

  • I am having major problems with Firefox. About 3 weeks ago I started experiencing the display only when on Firefox for all web site, that the display would be shifted right about half a screen. It only happens on Firefox and happens for all web sites.

    I am having major problems with Firefox. About 3 weeks ago I started experiencing the display only when on Firefox for all web site, that the display would be shifted right about half a screen. It only happens on Firefox and happens for all web sites.

    This issue can be caused by the Babylon Toolbar 1.1.8 extension
    Start Firefox in <u>[[Safe Mode]]</u> to check if one of the extensions is causing the problem (switch to the DEFAULT theme: Firefox (Tools) > Add-ons > Appearance/Themes).
    *Don't make any changes on the Safe mode start window.
    *https://support.mozilla.com/kb/Safe+Mode
    *https://support.mozilla.com/kb/Troubleshooting+extensions+and+themes

  • Print the form only when valid

    Hello,
    I have a PrintButton on my PDF form which activates the form validation. The print dialog appears only when the form is valid. However, users still can print the form using the File Menu or Toolbar. I tried to deny printing in the PDFSecurity tab of the Form Properties and re-enable it once the form is valid (xfa.PDFSecurity.SetAttribute("1","print")) but it does not work.
    Is it possible to force the user to print only with my print-validate button? What would be the appropriate way to do that?
    Thanks
    Julie

    Hi Johnny,
    The only way that Acrobat or Reader can vouch for the integrity of the digital signature is from within the application. Once you print the file Acrobat/Reader has no way of verifying that the printed document is cryptographically sound (which is what a digital signature proves). As such, we specifically inhibit printing the signature status.
    Another way to think of this is, the validity of an electonic document can only be guaranteed in the electonic environment. Once the document moves into the physical world Acrobat (or any application that processes digital signatures) has lost control.
    Steve

  • How do I get my itunes to recognize my account, it seems to be stuck on my friend account even when it sent my ID in the account, only when I try to update an App, that my friend account appear.  Please advise

    How do I get my itunes to recognize my account, it seems to be stuck on my friend account even when my ID show up as sign in, only when I try to update an App, that my friend account appear.  I try to reset it on the computer and the phone itself, but no luck.  Please advise

    Many thanks lllaass,
    The Touch Copy third party software for PC's is the way to go it seems and although the demo is free, if you have over 100 songs then it costs £15 to buy the software which seems not a lot to pay for peace of mind. and restoring your iTunes library back to how it was.
    Cheers
    http://www.wideanglesoftware.com/touchcopy/index.php?gclid=CODH8dK46bsCFUbKtAod8 VcAQg

  • MBP (13-inch, early 2011) goes into hibernation instead of safe sleep upon closing the lid (only when using battery power)

    MBP details
    13-inch MacBook Pro, early 2011 (model identifier: MacBookPro8,1)
    Running Yosemite, OS X 10.10.1 (however, problem started while using Mavericks)
    2.5 GHz Intel Core i5
    4 GB memory
    Symptoms
    * the MBP goes into hibernation instead of sleep upon closing the lid on battery power (i.e.,  when AC adapter is NOT connected)
      -- problem does not occur when AC adapter is connected
    * putting the computer to sleep by other means does not induce the problem; these other means include:
      -- [i] option+command+Power,
      -- [ii] Apple Icon --> Sleep,
      -- [iii] in Terminal: pmset sleepnow
    * the battery is NOT getting drained at all; if I close the lid on 87%, the battery is still at about 87% when I get the MBP back up and running
    To be as clear as possible:
    (1) MBP is on and in use  (power source: battery); battery is at X%
    (2) the lid is then closed
    (3) the battery indicator light glows bright for about 10 seconds, then shuts off, and with it so does the MBP (all fans and sounds cease)
    (4) opening the lid and pressing any key on the keyboard does nothing (the computer is not sleeping)
    (5) to turn the MBP back on, I must hit the power button once; this brings up  and the gray screen and loading bar indicative of returning from hibernation
    (6) log in; battery is still at X%
    Attempted Solutions / Fixes
    There are Apple Discussions addressing similar MBP issues, many of which at least have work-arounds, if not full-blown solutions. However, I have tried many of the proposed work-arounds and solutions; nothing has solved my problem. Such attempted solutions include:
      * made sure EFI/SMC firmware was up to date
      * updated all software
      * logged in as a different user
      * booted in Safe Mode
      * reset the PRAM
      * repaired disk permissions using Disk Utility
      * repaired additional disk permissions in recovery mode (a la this chron.com advice)
      * ran the Apple Hardware Test multiple times (short test while plugged in, short test on battery, extended test while plugged in, extended test on battery)
      * booted from an external known-good OS on USB drive
      * reseated the RAM
      * used MemTest to test the RAM's health (it was good)
      * tested my battery's health with the app Battery Health (it is healthy!)
      * booted my computer with one stick of the MBP's original RAM at a time (figured "why not?")
      * booted my computer using other sticks of RAM
      * checked my power management settings using PMSET in the terminal (e.g., pmset -g); tried various hibernation modes (e.g., sudo pmset -a hibernationmode X, where X=0,1,3, etc)
      * searched/grepped through my power management logs and Console messages to look for googleable items (e.g., pmset -g log | grep WORD, where WORD=failure, sleep, etc);  still have not found help online
      * brought the MBP to a Mac Genius
    -- he ran some further tests, but found nothing;
    -- he re-installed my OS as a hopeful-hail-mary-just-maybe-last-ditch effort; the problem persisted!
    -- we both agreed that we had ruled out a software cause
    -- he offered me to leave my MBP to get fixed for a flat fee of $300.00 (which after a couple weeks of trying to figure this out is starting to sound more and more reasonable)
    Given that it is likely not a software issue, what hardware might it be? By booting from an OS on an external drive, I think I've ruled out a bad hard drive or hard drive cable. Also, given that inducing sleep in any way other than closing the MBP's lid does not cause the problem, it seems there might be a wire (or something) in the hinge section of the MBP that gets moved when the lid is closed possibly causing some kind of short circuit...or something.
    If someone with more experience interpreting Console messages, for example, can help, I'd be happy to provide some logs, or any additional information.
    Thank you for your time and consideration.

    Ok, this is a crazy-simple, quasi-embarrassing fix, but I'll post it anyway just in case anyone is having a similar problem (for example, this guy: Changes to pmset being ignored).
    Some time around late last August / early September (2014) this issue began happening for me (specifically, issue = closing lid while on battery power induces hibernation / shut down instead of sleep --- independent of hibernation mode). Maybe it was when I upgraded to Yosemite (not sure what the exact date of that was). As detailed in my original post, I frantically tried everything I could think of... At that time, however, I had quite a few presentations and posters to create and present at meetings and conferences, so I held off fixing it for a couple of months and returned to it this past week, where I tried a few more things listed above, but again to no avail...
    This morning, I simply tried fudging around with all my power management settings again (via pmset in Terminal). The simplicity of the fix surprised me. But now that I understand it, I know it is likely the same fix for other threads I've read, such as the one above:
                                        (TLDR in bold)
    (1) Take note of your power management settings (Terminal:  pmset -g custom)
    (2) Is the "standby" flag set to 0? If so, set it to 1. Wa-la! Done.
    This fix is crazy and embarrassing b/c I was so close to it for so long. I tried changing my hibernation modes multiple times (sudo pmset -a hibernatemode X, where X=0,1,3,25,etc). I read about manipulating other PMSET settings that helped other people with newer MBPs, like "autopoweroff" and "autopoweroffdelay" --- settings that I do not have on the MBP early 2011 (check your settings: pmset -g cap). And most amazingly of all, I even messed with the "standbydelay" setting, trying to make it so long that my computer should never go into hibernation... Yet, from what I can tell by looking over my notes, I never simply set "standby" from 0 to 1.
    There are interesting questions that arise due to this fix. For example, why did this problem persist on clean installs of OS Yosemite? Is "standby" being set to 0 the Yosemite default? I'm not sure. If it is, then Kappy was right: this was normal behavior... It just wasn't always the normal behavior...at least not for my MBP. Normal behavior (hibernatemode 3) used to be (and now is again) that the MBP goes into safe sleep, and then only after an allotted amount of time will it go into full-blown hibernation.
    NOTE: the "standby" and "standbydelay" PMSET settings are not available in some earlier MBP models (e.g., my brother has a 2010 and does not have these settings); so if you're having a similar problem with an earlier model, this fix might not be of any help to you.

  • How to make a specific tab the active one when loading a page.

    We need to be able to navigate between a set of pages all containing the same tabs. When going from one page to another, the active tab on the new page must be "the same" as the active tab on the previous page. In other words: there must be a mapping between one page's tab and another page's tab. To do this we need to tell the next page which tab to set as selected. Is there any way of doing this? Because we use a left-side that is tab-independant for navigation, we can not hard-code the tab-reference in the hyperlink. We have thought about using a page parameter saying which tab is active (by the use of a tab id), but it seems impossible to make a portlet change the value of a page parameter without doing so with an event. Because the content under each tab is passive, we can't send any events. Is there any other way of doing this?

    When I click on the x to close a window, all windows are closing.
    When I click on an active link to open a new window, nothing happens , only the already pen one gets replaced. It' s a pain in the butt, and there is no help or solution from FIREFOX, I studied all help resources. I will not attach screen shot to this mail, as that wouldn't help anything. My solution - I will work with SAFARI or SEAMONKEY instead and not use FIREFOX any longer.

  • Memory sensitive cache? (Clearing Softreference only when necessary)

    I have a number of objects that I load into memory from files. These are large objects, and I cannot have all of them in memory at the same time. However, because they take time to load, I would like to load them in as seldom as possible. SoftReferences seemed to be an ideal way to do this, except that the objects pointed to by my SoftReferences are being finalized long before I run out of available memory.
    Is there a way to have these references hang around as long as possible? Ideally, they'd only clear if an OutOfMemory error is about to be thrown.
    Mark McKay

    Well, I am having the exact same issue -- and only one month after you are. I have objects, loaded slowly from xml files, which i cache in memory. Typically there will only be dozens or hundreds of them, but sometimes there will be thousands or tens of thousands, and I would like to improve my cache (which is currently just a Hashtable and a wrapper) to be contextually memory-sensitive.
    I'm confused as to the difference between a SoftReference and a WeakReference, especially since it seems to me that a SoftReference is the way to go but, as I look around the web for advice, most people seem to prefer WeakReferences. Since I'm leaning toward SoftReferences, I guess I don't want to use a WeakHashMap, rather I want a SoftRefHashtable of some kind, which perhaps I will have to implement.
    If I roll my own, is it as simple as overriding get() and put() to wrap the parameters in SoftReferences, and unwrap them back into objects?

  • I just updated my iphone to ios5 now when i go to music it says 'no content' when i checked my capacity it shows 7.9gb in 'other' . i can also see all my music is on the 'device' only when i go on the computer

    i just updated my iphone to ios5 now when i go to music it says 'no content' when i checked my capacity it shows 7.9gb in 'other' . i can also see all my music and apps on the device menue in itunes.

    I went to the Summary page of iTunes and checked "Sync only checked songs and videos".  Synced again, and no songs were playable.  Unchecked my only playlist and synced again.  All the songs were gone as expected.  Checked my playlist again, and all the songs were there.  Looked at them under the music tab, but they were all grayed out.  Tried to play any of them, but could not.  Tried to sync again and received the message, "The iPhone could not be synced because the sync session failed to start."
    Every time I connect my iPhone 4 to iTunes, it says "iTunes was interrupted while restoring the contents of the iPhone ____.  Would you like to restore this iPhone again now?"  I click "Continue Restore" (I'm tempted to click "delete backup.)  It then says, "An error occurred while restoring this iPhone (-54).  I click OK.  I'm redirected to the "Set up your iPhone screen".  It's already selected to "Restore from backup of ___'s iPhone".  Click continue.  It restores for about 2 minutes.  iTunes says my settings have been restored and wait for it to restart.  And again the message that "iTunes was interrupted..." (see above).  Crap.
    How do i get this back to where it belongs?

  • I can't install the new software 8.2 wireless from the phone only when I connect to my laptop

    need help to install the new software 8.2 wireless from my phone

    Howdy Punya,
    It sounds like you are unable to get your phone updated by downloading it directly to the device, and it only works when you connect it to iTunes. I would recommend you use the troubleshooting in the following article with different troubleshooting depending on the issue that it is having:
    Resolve issues with an over-the-air iOS update
    If that does not resolve, or doesn't address the issue, can you elaborate on what is happening when you try?
    Thank you for using Apple Support Communities.
    Regards,
    Sterling

  • Ask user filesystem access permission only when necessary

    Hello
    Sorry I can't find an explicit title.
    I try to remove my applet signature to avoid the startup security dialog which is not user-friendly.
    But my applet offers the possiblity to save something on the user file system that can't be done without signed applet.
    Is there a way to ask the file system access permission just when it is mandatory, like the PrintUtilities.print() does?
    Thanks you in advance

    Hi ,
    Thanks for you reply.
    I will try this out and will let you know the results.
    Are you talking about "setting default home page" for the user ,in your last line of reply ?
    I think , this is the case when user clicks on Home link then this default page will be rendered. Am i right ?
    Is there any docs which can tell all about the Access Privileges and also tells about how to play with Role Based Security in Oracle Portal Server ?
    Thanks Again
    <Neeraj Sidhaye/>
    Try_Catch_Finally AT YAHOO DOT COM
    http://ExtremePortal.blog.co.uk

  • Printing in Black only when necessary-then color

    I had  HP 9800 software installed by HP for my HP Officejet 4600 series printer . I would like to print text in black only. How do I do this?
    This question was solved.
    View Solution.

    No problem and great to hear.
    Could you please click "Accept as Solution" if you feel my post solved your issue, it will help others find the solution. Click the "kudos thumbs up" on the left to say thanks for helping! Thank you.
    I worked on behalf of HP.

Maybe you are looking for