Monday, January 30, 2012

Discussion for Jan. 30th

Today we talked about access control, package hierarchy, and class path.

Discussion Starters (respond for participation credit):
Pick one or more of the following to answer. If you're unsure about a question, google it, post your response, and cite your source. If possible, give examples via pastebin.com links.


What access levels does Java provide?
What does each mean?
For member variables, when would you want to mark them private?
Is there a good reason to mark them public? Explain your reasoning.

What does the package declaration mean?
Why do packages exist in Java?
Which package(s) are loaded by default in java?
How do you include an entire package in a class you write?
How do you include just a single class from a package in a class you write?
In a unix shell, how do you check your class path? How do you change your class path?
In the Java API there are two List classes*, java.awt.List and java.util.List. If you want to use both of these in the same class, how would you do it?

See you Wednesday!

Joe

P.S. 
Don't forget to do your pre-lab for the February 1st lab.

38 comments:

  1. For the last question, the easiest way to be able to use the two list classes within the same class would be to reference them using fully qualified name instead of an import statement. So you're code would read 'java.awt.List awtList = "blah"' and java.util.List utilList = "moreblah"' instead of just List.

    ReplyDelete
    Replies
    1. That is correct. You don't have to import the package to use the classes. You can simply refer to them by their full name. This is why you need to use the full name to launch a java application.

      Delete
  2. What access levels does Java provide?
    What does each mean?

    Access Levels:

    Modifier Class Package Subclass World
    public Y Y Y Y
    protected Y Y Y N
    no modifier Y Y N N
    private Y N N N

    http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

    I have also heard the "no modifier" modifier (if that makes
    sense) be called a "packaged private" modifier.

    Public - unrestricted access
    Protected - Accessible from own package and derived classes
    Private - Accessible inside containing class
    Packaged Private - Accessible from own package

    Why do packages exist in Java?

    To stay organized. Variables that are named the same are not conflicting. So you can reuse and build upon code you use often.

    Which package(s) are loaded by default in java?

    The only one I know of is the java.lang package.

    How do you include an entire package in a class you write?

    Not sure exactly, but I think you can just add an asterisk to the end of the package name when importing it.

    How do you include just a single class from a package in a class you write?

    Not sure exactly, but I think you can either be specific in the package path or say that you class extends another class if they are in the same package.

    ReplyDelete
    Replies
    1. Nice answer Vivek. Make sure you use the correct terms. You said, "Variables that are named the same are not conflicting." You actually mean Classes not Variables.

      Tom asked the question, "Can you access private member variables anywhere in the containing file?" What do you think? Can you think of an example where you might not be able to access a private member variable in the same file?

      Delete
    2. Yeah sorry, I typed in the wrong term. I think yes you can access private member variables anywhere in the containing file as long as they are within the same class. The only example that I can think of where you might not be able to access a private member variable in the same file is if you have multiple classes within the same file. I have a slightly off topic yet related question. If you create a java file with multiple classes, how do you run the classes that don't have the same name as the file?

      Delete
    3. It depends on where and how the class is declared. Could you be more specific?

      Delete
    4. "Can you access private member vaiables anywhere in the containing file?"

      You can if you declare them at the start of the class, even if they don't have an assigned value yet. However, if you declare a variable inside of a block, then you can only see the variable in said block, not outside of it. For example, if you declare a variable inside of an "if" statement, you can't see that variable anywhere else but within that "if" statement.

      Delete
  3. Why would you want to mark a varible private?
    The answer is if you want to have a class where you only want people of access the varibles through the file and not from anyother classes or packages.

    http://sip.clarku.edu/tutorials/java/java.html

    ReplyDelete
    Replies
    1. Your answer is confusing to me. Could you clarify this?

      Delete
    2. Basically you want a private variable to limit what people can do to it. For example is you have a class that has your name in it, you dont want it to be change by other classes so you use a private variable so they can't.

      Delete
    3. I think I understand what you're trying to say. However, you can access and even change private variables in a certain class from another class. You just need a method call for it to be possible. It's a way of double-checking the user, forcing them to say, "Yes, I really do want to change this."

      Delete
    4. Moon: You are correct in saying that if you make a variable private you can limit what people do to it. You do indeed limit what other people can do to it. However, make sure you understand that just because the variable is private, that doesn't mean it cannot change. For example, if you are building a program that counts by 2's, you wouldn't want to give access to the variable to other people. Otherwise they would of course try to be boneheads and increment the variable by one instead of two like you wanted. As Jennifer was trying to say, you can provide a method (in this case it could be called 'increment()') that way the variable changes in a way you expect.

      So, Jennifer was 1/2 right in saying you can still change the private variables via a public method that was provided in that same class. However, I wouldn't say this is a way of "double-checking" the user. This is not meant to "double-check" what the user wants to do to the variable, but more a way of protecting code and making it act in a manner you define. In the example above, the public method called 'increment()' forces your private variable 'count' to act in a specific manner.

      Delete
    5. Mr. Moon, that is correct. If you only want to allow people to modify your variables through the provided interface you mark it private.

      Delete
  4. Dean Dominguez:

    In a unix shell, how do you check your class path? How do you change your class path?

    He did this in class also when he was showing us his class path. Use command "echo $CLASSPATH". To change class path use command "setenv CLASSPATH /your/path/not/anyone/elses/".

    ReplyDelete
    Replies
    1. The problem is that the class paths change back to default once you log out and log back in, right? Also, I'm not sure how this could be useful, unless you were beta testing another class with the same name but you weren't ready to overwrite the old class version.

      Delete
    2. There are ways you can have a class path added each time you start your shell. I suggest googling "bashrc".

      Delete
    3. I've modified my bashrc file and I've noticed that it does not load when I log in to moons.cs.unm.edu. There are a couple of ways around this. Number one is to add your config into .bashrc and simply type "source .bashrc" (without the quotes) from your home directory to load the changes. Not to elegant, but it works. The better solution is to add that line to your .bash_profile file. If you don't have that file, simply create it with the line above, save it, log out and log back in and you're ready to rock.

      Delete
    4. Does the moons server not have the bash shell? When you start the bash shell (oddly enough by typing "bash" at the terminal), it automatically looks at the commands stored in .bashrc.

      Delete
    5. I think it uses bash by default but there may be a special configuration that does not execute all the default bash files on connection via ssh.

      Delete
  5. "What access levels does Java provide?"

    Access level modifiers determine whether other classes can use a particular field or invoke a particular method. There are two levels of access control:

    At the top level—public, or package-private (no explicit modifier).
    At the member level—public, private, protected, or package-private (no explicit modifier).

    A class may be declared with the modifier public, in which case that class is visible to all classes everywhere. If a class has no modifier (the default, also known as package-private), it is visible only within its own package (packages are named groups of related classes — you will learn about them in a later lesson.)

    At the member level, you can also use the public modifier or no modifier (package-private) just as with top-level classes, and with the same meaning. For members, there are two additional access modifiers: private and protected. The private modifier specifies that the member can only be accessed in its own class. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.

    Source: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html


    As you can tell, these are not my own words. I am still very confused about how to use Java and the EMACS editor. I need to meet with a TA as soon as possible so I can succeed in this class.

    Jeff

    ReplyDelete
    Replies
    1. Make sure you come by office hours. You say you need help but I have not seen you yet. Hours are available on the calendar page of the main site.

      Delete
    2. Agreed, I have been awful lonely in my 6-7 office hours. If you can make it, we would be happy to help.

      Delete
  6. I just thought I'd choose some random questions and attempt to answer them.

    "How do you include an entire package in a class you write?"

    I think the fastest way to include and entire package would just be to import it. For example, if you had a package called "CelestialBodies", you would just type at the top of your code...

    import CelestialBodies.*;

    then you have access to all of the classes in that package in the class you've imported it into.

    "How do you include just a single class from a package in a class you write?"

    There are two different ways to get a single class to your class (that I'm aware of) You can either import it like I did above, except for naming which class of that package you would like. Like if there was the class "HotBodies" in the package "CelestialBodies" you would import by typing...

    import CelestialBodies.HotBodies;

    Another way to get the class HotBodies is by declaring its fully qualified name.

    CelestialBodies.HotBodies Aaron = new CelestialBodies.Hotbodies();

    I think this would give you access to that Class without needing to import it. My question is though, why exactly would you need to import an entire package into your class? Is that really only necessary for those really big programs?

    ReplyDelete
    Replies
    1. It is generally used if you are going to use many classes that exist in the same package.

      Delete
    2. http://www.jarticles.com/package/package_eng.html oh and here's the source I used.

      Delete
  7. "For member variables, when would you want to mark them private?"

    Not a 100% on this one, but I'll try. You would want a member variable to be private when the variable is responsible for implementation of the class. For instance, for a class Hat, the variable numOfStitches might be private because, while it is a property of a hat, it is one that is generated based on other parameters of the the hat, such as size and shape, rather than being defined explicitly for each hat.

    Another reason for a member variable to be private is if the value of the variable needs to be restricted in some way. For instance, if typeOfHat is a String, and only certain values will work, such as "Bowler," instead of having other classess manipulate the string directly, it could be made private and only changeable through interface methods to ensure the data remains valid.
    --------------
    "How do you include an entire package in a class you write?"

    You would use an import statement with a * wildcard. For instance:

    import java.util.*;

    would import every class in the util package.
    --------------
    "How do you include just a single class from a package in a class you write?"

    You would use an import statement that points specifically to the class file you want. For instance, if you wanted the Locale class, you could write:

    import java.util.Locale;

    and that would give you access to just the Locale class from the util package.

    ReplyDelete
    Replies
    1. This is a pretty good explanation. Is there really a good reason to not mark a member private?

      Delete
    2. The only thing I can come up with is for constants (final). Making a constant public seems safe and useful at a glance, but I've not attempted to break it.

      Delete
    3. This is one way that was acceptable pre Java 5 for declaring enumerated types.

      However, this is now generally frowned upon. When you make something public, you are committing to having it in your code forever if you release it. If you make it private, you can potentially change the internal workings without having to change your interface.

      There are actually several things in Java history that due to backwards compatibility requirements can not be removed. I highly recommend looking up "deprecated java" and seeing what parts of the API were not fully thought out before release.

      Delete
  8. to find the classpath in the unix shell dont you just type echo classpath and the file

    ReplyDelete
  9. I am a little bit sad I came late to this party, seems most of the questions have been answered. I must say reading through this actually cleared up allot of confusion I was having. The only questions I knew from the start were how to import an entire package using the * wildcard and how to import a class. Though we have been doing so much with the private modifier I had a good idea. I just feel that was the only question who's answers were all a bit hard to understand. I would just say that you would make a variable private if there are values that you do not want that variable to take on. By making it private only the setter method can change it and you can filter out the values you don't want.

    ReplyDelete
  10. I've found that for member variables, you would want to mark them private whenever you either don't want someone else to access them directly or when you want someone to go through a method first.

    ReplyDelete
    Replies
    1. How about both! I am a huge advocate of "mark everything private". If you then need to reduce the level later because there is no way around you can.

      Delete
  11. Thought I would just answer some of the questions that I am sure of.
    -Why do packages exist in Java?
    They exist so that you can import one package and be able to use all the classes within it instead of importing each class individually.
    -Which package(s) are loaded by default in java?
    java.lang
    -How do you include an entire package in a class you write?
    ex: import java.util.*; this imports all the classes in the util package.
    -How do you include just a single class from a package in a class you write?
    import java.awt.Color; imports just the color class of the awt package.

    ReplyDelete
    Replies
    1. Can you think of any other reason packages exist in Java?

      Delete
  12. In a unix shell, how do you check your class path? How do you change your class path?

    You can use the command "echo$ [CLASSPATH] to check your class path.

    To set or change a class path for Java in Linux you can simply export CLASSPATH="your classpath" from your .bash_profile.

    ReplyDelete
  13. -Is there a good reason to mark them (member variables) public? Explain your reasoning.

    Well maybe not just public: if the variable is defined by the constructor you could declare it both public and final. Then you could access the value of the variable directly (without a method) from other classes (as in array.length).

    I think you need to very careful with declaring member variables public though. This is because you could create instability in your program by letting other classes change the values your class is working with and you are restricting the changes you could make to the class later (contract). For the most part it's better to declare a variable private, and then use getter and setter methods as needed.

    ReplyDelete
    Replies
    1. Pre-java 1.5 I would agree with your first part about public final, if you added a static in there too. In Java 1.5 they introduced enumerated types. Prior to this, they would create static constants to represent these. In my opinion, there is currently not a very good reason to make any of your member variables public because you can ALWAYS create a getter to access them. This allows you to change the inner workings of your code without necessarily changing the contract.

      Delete