Download Public or Private -

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Java syntax wikipedia , lookup

Design Patterns wikipedia , lookup

Java ConcurrentMap wikipedia , lookup

Java (programming language) wikipedia , lookup

Java performance wikipedia , lookup

Class (computer programming) wikipedia , lookup

Name mangling wikipedia , lookup

Object-oriented programming wikipedia , lookup

C++ wikipedia , lookup

C Sharp (programming language) wikipedia , lookup

C Sharp syntax wikipedia , lookup

Transcript
HC Computing
[email protected]
Keywords and Resources
Public or Private Controlling Access to Members of a Class
You will notice that most classes we use have keywords like:
public static class myClass {
.
.
.
}
Also the attributes and methods of the class may have keywords associated
with them.
private int counter = 0;
public int siteHits = 0;
public static void main(int argc, String[] argv)
{
.
.
.
}
Java uses three explicit keywords and one implied keyword to set the boundaries in a
class: public, private, protected and the implied “friendly,” which is what you get if
you don’t specify one of the other keywords. Their use and meaning are remarkably
straightforward. These access specifiers determine who can use the definition that
follows. public means the following definition is available to everyone. The private
keyword, on the other hand, means that no one can access that definition except you,
the creator of the type, inside function members of that type. private is a brick wall
between you and the client programmer. If someone tries to access a private member,
they’ll get a compile-time error. “Friendly” has to do with something called a
“package,” which is Java’s way of making libraries. If something is “friendly” it’s
available only within the package. (Thus this access level is sometimes referred to as
“package access.”) protected acts just like private, with the exception that an
inheriting class has access to protected members, but not private members.
Use private unless you need to expose the method or attribute to another class or the
class to another class.
The Static Keyword
There are three places where data can be traditionally stored in a running program:



Rev 00
On the stack (automatic variables)
o They only live for the live of the method
On the heap (for the life of the object)
Static Data Storage (static variables)
Page 1 of 3
HC Computing
[email protected]
Keywords and Resources
Ordinarily, when you create a class you are describing how objects of that class look
and how they will behave. You don’t actually get anything until you create an object
of that class with new, and at that point data storage is created and methods become
available.
But there are two situations in which this approach is not sufficient. One is if you
want to have only one piece of storage for a particular piece of data, regardless of how
many objects are created, or even if no objects are created. The other is if you need a
method that isn’t associated with any particular object of this class. That is, you need
a method that you can call even if no objects are created. You can achieve both of
these effects with the static keyword. When you say something is static, it means that
data or method is not tied to any particular object instance of that class. So even if
you’ve never created an object of that class you can call a static method or access a
piece of static data. With ordinary, non-static data and methods, you must create an
object and use that object to access the data or method, since non-static data and
methods must know the particular object they are working with. Of course, since
static methods don’t need any objects to be created before they are used, they cannot
directly access non-static members or methods by simply calling those other
members without referring to a named object (since non-static members and methods
must be tied to a particular object). To make a field or method static, you simply
place the keyword before the definition. For example, the following produces a static
field and initializes it:
class StaticTest {
static int i = 47;
}
Now even if you make two StaticTest objects, there will still be only one piece of
storage for StaticTest.i. Both objects will share the same i. Consider:
StaticTest st1 = new StaticTest();
StaticTest st2 = new StaticTest();
At this point, both st1.i and st2.i have the same value of 47 since they refer to the
same piece of memory. There are two ways to refer to a static variable. As the
preceeding example indicates, you can name it via an object, by saying, for example,
st2.i. You can also refer to it directly through its class name, something you cannot do
with a non-static member. (This is the preferred way to refer to a static variable since
it emphasizes that variable’s static nature.)
StaticTest.i++;
The ++ operator increments the variable. At this point, both st1.i and st2.i will have
the value 48. Similar logic applies to static methods. You can refer to a static
method either through an object as you can with any method, or with the special
additional syntax ClassName.method( ). You define a static method in a similar way:
class StaticFun {
static void incr() { StaticTest.i++; }
}
Rev 00
Page 2 of 3
HC Computing
[email protected]
Keywords and Resources
You can see that the StaticFun method incr( ) increments the static data i using the
++ operator. You can call incr( ) in the typical way, through an object:
StaticFun sf = new StaticFun();
sf.incr();
Or, because incr( ) is a static method, you can call it directly through its class:
StaticFun.incr();
Although static, when applied to a field, definitely changes the way the data is created
(one for each class versus the non-static one for each object), when applied to a
method it’s not so dramatic. An important use of static for methods is to allow you to
call that method without creating an object. This is essential, as we will see, in
defining the main( ) method that is the entry point for running an application. Like
any method, a static method can create or use named objects of its type, so a static
method is often used as a “shepherd” for a flock of instances of its own type.
If you do not use the static keyword, the variable is placed on the stack for a function
and on the heap for an object. This determines the lifetime of the variable.
Resources for Learning Java
1. Java Programming Tutorial from the horse’s mouth. Sun’s very easy to
follow and clear tutorial on Java Programming.
http://java.sun.com/docs/books/tutorial/
2. Using Swing – The GUI libraries for Java. This is not covered in this
module, but is provided as a reference for further study.
http://java.sun.com/docs/books/tutorial/uiswing/
3. Object Oriented concepts. As you know, the main advantage of using
this approach allows us to model real life problems in a relatively
straightforward manner. Again this is from Sun Microsystems, designers of
Java
http://java.sun.com/docs/books/tutorial/java/concepts/
4. Thinking in Java – A free book by Bruce Eckel.
http://www.mindview.net/Books/TIJ/
5. Integrated Development Environments (IDE)
a. BlueJ – http://www.bluej.org
b. Net Beans - http://www.netbeans.org/
6. Java Development Kit (JDK). This can be downloaded from
www.javasoft.com. You need to Standard Edition (SE), version 5 or
greater. http://java.sun.com/javase/downloads/index.jsp
Rev 00
Page 3 of 3