Access control — private & public

We used modifiers such as public and private previously as describing field and method. Modifiers are used to specify the access authority to field and method. Java checks whether it can access the fields and method when it’s accessing object’s field or calling a method. Modifiers such as private and public are used to specify who can access the method or field.

access-level

Java Access Level for Members: public, protected, private

There are 4 types of access control modifiers.

  • public
    • Anything can access to field and method
  • protected
    • the class itself and its subclass can access to it
  • none
    • anything in the same package can access to it
  • private
    • only class itself can access to it. Anything else can’t.

For example, the following code defines private “name” field, public “version” field, and public “changeName()” method.

Snip20150519_13

Suppose there is another class “Cafe” class that uses User class, and it accesses to two fields directly as shown below.

Snip20150519_14

Since “version” is public, it is accessible in all classes. But name field is private, so only User class can access to it. If another class access to private field or method as above, it will result in compile error.

import keyword

When using a class contained in the same package, you can directly use the class name in the source code. For example, in the following example, MemberInfo class and Address class are in the same package. In MemberInfo class, you can use Address class using simple name.

Snip20150519_5

If you are to use a class that belongs to another package, you must write the full qualified name of the class. For example, to use MemberInfo and MemberDao class in MemberManager class in java.manager package, you must use fully qualified name as shown below.

Snip20150519_6

Giving full qualified class name every time is cumbersome, so you can import keyword to use classes in other packages. The code below shows an example using import statement. import is located between package and class definition.

Snip20150519_7

If you specify fully qualified name of class next to import, you can use that class using simple name.

If there are too many classes to import from package, you can import the entire class of package using “*”. The following is example.

Snip20150519_9

Set of class package and class names

When developing a big software, programmer sometimes make hundreds to more than thousand classes. It will be difficult to maintain the program if all classes are placed in same directory. In this case, it will be slightly more efficient to manage the source code and classes if locating them into several directories.

Java distinguishes classes using the same structure as a structure of directory. We call this unit to tie a set of classes a “package”, and package can contain a package or a class.

package

This picture shows the configuration of package and the relationship between package and class. In above picture, package “java” contains another packages “lang”, “util”, and “awt”, and these three packages contain classes. In other words, package can contain sub-packages or classes. In this case, the full qualified name of package “lang” is java.lang .You use dot(.) to show the relationship.

Package has a same structure as a structure of directory. In other words, java.lang package is connected to lang subdirectory in the java directory. For example, suppose we store class files in “classes” directory. In this case, java.lang package is connected to classes\java\lang directory, and java.lang.String class is associated with String.class file in classes\java\lang directory.

To show which package a class is included, use “package” statement.

Snip20150519_4

Package statement has to be the first statement of a source code. But comment can come before this.

How to create an object and how to use it.

If you defined source code for class and created class file, you can create an object using that class.As previously deccribed, when you create an object, you need “new” and constructor.

Point center = new Point(0,0);

If you assigned the object to the reference, you can use the field using reference or call the method. When accessing field, use the following format.

ObjectReference.fieldName

The code below shows an example of reading the value of field and assigning a new value to the field.

Snip20150518_6

When calling method, use following format.

Reference.MethodName(parameterValue)

Here is example of calling method of an object.

Snip20150518_7

Value of reference becomes null if it doesn’t refer to any object. If you are trying to access to field or call a method using a null reference, Java generates NullPointerException exception. Therefore, when you approach a reference using a reference, be careful not to make it null.

Snip20150518_8

Java Class – Constructor

Finally. the component of class we will look at is Constructor. Constructors are used when creating an object of class, and it has a similar format as method. The difference is that it runs when creating an object and it does not have a return value. The syntax of constructor is as follows.

accessModifiers className (parameterList) {

}

The value of access modifiers will be something like public and private. Constructors always have the same name as the class name. The code below shows an example of MemberInfo class constructor.

Snip20150518_3

Constructor is used when creating an object with “new”. The following code is an example creating object using MemberInfo constructor.

Snip20150518_4

If you run a constructor using “new”, object is created. In other words, in above code, if you run a MemberInfo() constructor, MemberInfo() object is created and it is assigned to “info” reference. If you create an object, you can use it using a reference refering to the object.

Java Class – method

Method defines functionality provided by object. The method usually changes value of field or perform an action using the value of field. The syntax defining the method is as follows,

[Modifier] [returnType] methodName ([parameter list)] {

    Java code;

    return ___;

}

Here is an example of simple method.

Snip20150518_1

The second setBirthYear() method’s first parameter name and field name are the same “birthYear”. When the name of parameter and field are same, you have to use this keyword to access field within the method. If you do not use “this” keyword in Java, it recognizes it as a parameter. If the names differ, you don’t have to use “this” keyword.

If you define a method of a class, you can call the method after creating an object of class. For example, the code below shows calling method after creating object.

Snip20150518_2

How to define class in Java and what is field?

Defining class and its configuration

When defining class, it follows this syntax.

Snip20150517_9

Public means that the class can be used in other class. ‘Class’ means it is defining a class. After ‘class’, class name is located and between brackets, body of class is located.

The body of class is composed of field, constructor, and methods as follows:

Snip20150517_10

1) fIeld

The field stores information of the object. For example, in a object representing member information, field is used to store member’s name and date of birth. Defining fields have following syntax.

[Modifier] [fieldType] [fieldName];

In modifier, access control modifiers like private and public are mainly used. In Field type, default data type or class type is used. Field name is used to distinguish fields.

For example, in above picture, there are two fields defined. The first one’s modifier (access control) is private, type is string, and its name is “name”. The second one has a type “int” and its name is “birthYear”.

Name of the field must be made in accordance with the rule. Rules are as follows:

  • Field names are case-sensitive.
  • field name can be combination of unicode character and numbers. There is no limit to the length.
  • The first letter of name must be a letter, ‘_’, or ‘$”. However, ‘$’ is rarely used, and most coding rules are constrained to use only letters and ‘_’.
  • In general, if the name is composed of several words, the first letter of second word is capitalised.

According to the above rules, ‘name’, ‘name1’, ‘_name’ are correct, but ‘1name’ ‘%name’ ‘name%’ is not the correct name. If you do invalid field name, Java generates compiler error. If you want to define a value to a field, you may specify in advance. For example, if you want the default value of birthYear field to be ‘1970’, you can allocate 1970 to birthYear with the assignment operator ‘=’.

private int birthYear = 1970;

If you do not specify a value of the field, the default value is assigned to each type. If the data type is an int or long, integer 0 is allocated. If the type is float or double, 0.0 is allocated. For the class types like String, null is allocated.

 

 

Java — class (1)

Java provides features that can be used using class. For example, earlier we manipulated texts using string class, we used java.util.Date class to show time. There are many different classes. Programmer can directly create a class of his own too.

Typically, it helps us to efficiently develop a program if we use classes that provide many features and JSP together, rather tan developing 100% with JSP. It is also helpful to maintain the code after it is finished.

Object and Class

Java is an object-oriented language. Object oriented language has variety of features. The most important concept is that objects providing different features are connected to each other to provide necessary features. Here is an example.


  1. Information Form Object
    • sign-up page form
    • asks sign-up object to process this data
  2. sign-up object
    • checks for ID
    • handles sign-up process
    • asks DB to store datas
  3. DataBase object

Form object provides sign-up form, and when sign-up request comes in, rather than processing the registration, it asks sign-up object to handle it. Sign-up object who received the request does various logical processing required for registration. then it asks Database object to store the data. Then Database object adds relevant data into the member table.

Automobile is consist of different parts such as engine, brake, and tire, and they are connected to provide functionality needed. Likewise, different objects are linked together to provide necessary functions. In fact, programming in Java is a process of creating various objects and properly connecting these functions to write a code that provides right functions.

Objects have unique properties and provides functions. For example, objects that contains member information includes informations such as member’s ID, name. Object that has ‘Minjoo Kwon”s information and ‘Jin Namgoong”s information both contain ID and name. In other words, each has different values, but contain same type of informations.

So how can we know which object has what value and offer what features? It is class that is used at this case. Class contains information about the object — what kinds of information and what function it provides.

With the class, we create an object, and that object will have the information defined in class. Object-oriented languages such as C# and C++ including Java use class to define object and create an object.

Java — comment

After writing a source code, you might have to look at it after 2 weeks or 2 months. Then, it is possible that you won’t understand the code very well even though you are the author. So, in order to make it easy to understand even later, you write an explanation to code. We call it comment.

There are 3 kinds of comment you can use in JSP.

  • JSP comment
  • Java comment
  • HTML comment

JSP comment 

JSP comment starts with <%– and ends with –%>

ex) <%– This is a comment –%>

It doesn’t matter what comes between <%– and –%>. But, if comments are superimposed like this, only the statements between first <%– and first –%> are treated as a comment. The last –%> are included as an output. Only bolded statements are treated as a comment

<%– <%– comment –%> –%>

When you run JSP page, the comment part will not show on the output. For example,

Snip20150516_13Snip20150516_14

Snip20150516_20

— source code of the page.

Java Comment

Java comment is used in script. It is treated as a comment in Java. There are three types of java comment.

  • //comment
    • comments out everything after // in the current line.
  • /* comment */
    • comments out statements between /* and */.
  • /** comment */
    • comments out statements between /** and */.
    • Used only when commenting on method defined in declaration.

Java comments are used only within scriptlets, expression, and declaration. if used outside of those, it will not be recognized as a comment. Here is an example using java comment.

Snip20150516_15

Snip20150516_16

HTML comment

HTML comment is comments on HTML code, which is used mainly for describing the design elements, rather than a description of a program. HTML comments have the following syntax:

In contrast to JSP and Java comment, HTML comments are included in the source code. Because HTML comments are treated as a HTML code in JSP, there is no problem with using script element in html comment.

<! — Current user: <% userID &>. –>

Here is a example code for HTML comments.

Snip20150516_17

Snip20150516_18

Snip20150516_19

This is a source code of HTML page.