For Programmers: Free Programming Magazines  


Home > Archive > Cobol > March 2006 > Re: Java compatibility issues (WAS: MF having issues?)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Java compatibility issues (WAS: MF having issues?)
Oliver Wong

2006-03-08, 6:55 pm

[slight reordering post the post]

"James J. Gavan" <jgavandeletethis@shaw.ca> wrote in message
news:AqvPf.121958$H%4.62432@pd7tw2no...
> You Java boys convey how static gets used in Java, to counter what I've
> written above.


I'll give it a shot, but I wasn't able to follow everything.

>
> ----------------------------------------------------------------------
> Data Inheritance
>
> A factory object inherits all the factory data of its superclass,


I guess the same thing happens for "static data" in Java, though I
wouldn't call it "inheritance". On the other hand, I don't know what I would
call it.

> and an instance object inherits all the instance data of its superclass.
> The inherited data of a subclass includes any data that the superclass
> itself inherited from a class higher up the hierarchy. The inherited
> object data is initialized when an object is created.


This is exactly the same in Java.

> The inherited factory data is allocated independently from the factory
> data of the inherited class or classes when the factory of the subclass is
> created.


Not sure if I understood this, but I think it's basically saying that
factory data is shared across all instances, which is the same in Java.

> A subclass can only access inherited data through the methods it inherited
> from the same class. For example, if the class Manager inherits from the
> class Employee, and Employee defines the data item:
>
> employeeNumber PIC 999
>
> The Manager class cannot store a value in its inherited employeeNumber
> data item by using a statement such as:
>
> move 100 to employeeNumber
>
> Instead it must use a method declared in the Employee class, for example:
>
> invoke self "setEmployeeNumber" using 100


Java (and C++ and C#) defines "access levels" which determine what is
accessible from where. I believe almost every OO language has a concept of
"public" and "private". Java additionally has "protected" and
"package-default".

The description of the access level for the data given above matches the
concept of "private". That is, only the class that declared "private" data
can directly access it. "public" means any class can access the data, and I
suspect that in OO COBOL, all methods are public.

"protected", in Java at least, means that this class, and all of its
subclasses can access the data. So if you declared employeeNumber to be
protected, then you could directly access it via the MOVE statement in the
subclasses, but other classes which aren't part of the hierarchy (and thus
which don't inherit the employeeNumber field) wouldn't be able to have
direct access.

Finally, "package-default"... In Java, you can group your classes into
packages. I haven't used package-default much (though I have been using
packages a lot), so I might have this wrong, but I believe it behaves like
"private", except that any class in the same package can also directly
access the field.

If I'm wrong, then it actually behaves like "protected", except that any
class in the same package also has access.

>
> The object reference SELF is a reserved name which always refers to the
> object in which it occurs (see the section ....


In Java, C++ and C#, the keyword is "this" (all lowercase, since they're
case sensitive) rather than "SELF".

> -------------------------------------------------------------------------
> The Factory Object Source Element
>
> The factory object source element defines the data and methods for the
> factory object. It is nested within the class source element, immediately
> following the class source element's Data Division (if there is one). It
> looks like this:
>
> factory.
> working-storage section.
> * factory data .
> ...
> * factory methods
> end factory.
>
> The factory object data is defined in the Working-Storage Section. ***The
> factory object data can only be accessed from the factory methods*** - my
> Asterisks.


Yeah, OO COBOL seems to explicitly be pointing out that the Factory
object and the Instance object are two distinct objects, while in Java, C++
and C#, there's only "one" object, of which some parts are "instance"-type
things, and some parts are "static"-type things.

Here's a Java file with 1 instance field, 1 static field, 1 instance method,
and 1 static method:

class Foo {
private int instanceInteger;

private static int staticInteger;

public int instanceMethod() {
return this.instanceInteger;
}

public static int staticMethod() {
return Foo.staticInteger;
}
}

Note that it doesn't make sense to speak about "this" object when in a
static method, because the static method is shared across all instances, so
it's not clear which object you're referring to when you say "this object".

Also note that for static fields, you can qualify them with the class
they belong to. This is because static fields are shared amongst all
instances of the same class, so if you just give the class name, you've
fully qualified which field you're talking about.

For instance fields, however, each instance has its own memory allocated
for the field, so if you said "Foo.instanceInteger", the compiler wouldn't
know which field you were talking about (and thus give you an error).

> ----------------------------------------------------------------------------
> Factory Methods
>
> Each factory method is a nested source element. The code below shows an
> outline for a "new" method for Stopwatch.
>
> method-id. "new".
> ...
> linkage section.
> 01 lnkWatch object reference.
>
> procedure division returning lnkWatch.
> * code to create and initialize a Stopwatch object.
> exit method.
> end method "new".
>
> As with the class source element itself, you can declare different types
> of data in the Data Division of the method. The DATA DIVISION header
> itself is optional. Data declared here is only accessible to the code in
> this method. The Data Division can contain any of the following sections:
>
> * Local-Storage Section
>
> Data items that are local to the current invocation of a method. The
> method uses local storage for all its temporary working data. This has the
> advantage of supporting recursion.


In Java, C++ and C#, you can declare new variables in the bodies of methods.
These variables are visible only to the scope they are currently inside (the
scopes correspond almost exactly to the curly braces {}). You don't specify
access-levels for these variables. Here's some an example:

class Foo {
public void method1() {
int x;
{
int y;
/*Can see both x and y*/
}
/*Can see x, but can't see y.*/
}

public void method2() {
/*can't see x or y from here.*/
}
}

> * Linkage Section
>
> Variables passed as parameters to and from the method.


The parameters to the method appear in the round parenthesis after the
method name:

class Foo {
public void method1(int parameter1, int parameter 2) {
/*does something*/
}
}

>
> The Procedure Division contains the code for the method. You terminate
> processing of the method with an EXIT METHOD statement. This returns
> processing to the program which invoked the method.


You use the "return" keyword to exit out of a method. Every method has a
return type. If the return type is "void", it means the method does not
return anything. If the return type is "int", it means the method returns an
integer. Etc. If the method return type is NOT void, then after the "return"
keyword, you must have an expression representing the value to return.

class Foo {
public void voidMethod() {
return;
}

public int returnsLiteral() {
return 3;
}

public int returnExpression() {
return 3 + 4 / 5 * returnsLiteral();
}
}

>
> To see the "new" method, use the Editor to locate the "new" method, below
> tag S015. This method uses a Linkage Section to return data from the
> method.


The "new" method is called a "Constructor" in Java, C++ and C#, and it's
a special kind of method (in fact, it's technically not considered a method
at all in Java). You don't specify a return type for constructors, as the
return type is the class itself. You also don't explicitly ever use the
"return" keyword. The constructor is often used to initialize fields.
Constructors can have accessors on them, so you can have a private
constructor, for example. The name of the constructor is always the same as
the name of the class.

To create a new object, you use the "new" keyword, followed by the
constructor you wish to invoke (an object can have more than one
constructor).

class Foo {
public int x;
public Foo() {
/*This is the constructor gives x a default value of 3.*/
this.x = 3;
}

public Foo(int specificValueForX) {
/*This constructor allows the user to choose an initial value for x*/
this.x = specificValueForX;
}
}

class Bar {
public static int methodThatReturns3() {
Foo f = new Foo();
return f.x;
}

public static int methodThatReturns5() {
Foo f = new Foo(5);
return f.x;
}
}

> The Instance Object Source Element
>
> The instance object source element defines the data and methods for
> instances of the class. It is nested within the class source element. It
> looks like this:
>
> object.
> working-storage section.
> * instance data for the object.
> ...
> * Instance methods
> end object.
>
> The only Data Division section that has any meaning in an object source
> element is the Working-Storage Section. You can create other data
> sections, but the run-time behavior if you try to access the data in these
> sections is undefined.
>
> Any data you declare in the Working-Storage Section is accessible to all
> the instance methods, and may be inherited by instances of subclasses of
> the class.
>
> There is no Procedure Division in an object source element, only methods.
> To write an initialization method for instances, write a method called
> "initialize", and then invoke it from the "new" method for the class after
> you have created an instance.
>
> To see the object source element and data declarations paragraph for
> Stopwatch, use the Editor to locate the OBJECT header. The OBJECT header
> and Working-Storage Sections are located below tags S030 and S035.
> Instance Methods
>
> Instance methods are nested inside the object source element. Writing an
> instance method is exactly like writing a factory method, with the only
> difference being the scope of data which the instance method can access.
>
> The instance method can access data:
>
> * Declared in the Data Division of the instance method source element
>
> * Declared in the Working-Storage Section of the object source element


This doesn't say much except the structuring of the source code, in
which Java obviously differs.

- Oliver

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com