473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Inner classes use

Currently I'm working on a class that performs a batch process as per
scheduling for all employees in a company. Being a batch process load
is obviously a huge factor.
The process first needs to calculate the eligibility of each employee
before proceeding to the next stage. I need to cache some of the
employee information if an employee is found eligible for later use in
the next stage.
One option could be a javabean style value object class with all
required attributes and getter setter methods. However I would like to
avoid the overhead of these method calls for say 3000 eligible
employees. A lightweight object where I can access the attributes
directly would help me

I therefore propose an inner class....

class Process {

doEligibility () {
EmpInfo a = new EmpInfo();
a.attrib1 = //set it
a.attrib2 = //set it

//store in hashmap with empID as key

}

private Class EmpInfo { //INNER CLASS
int attrib1;
String attrib2;
.....
}

doProcess {

EmpInfo a = (EmpInfo) map.get("empID");

//use the attributes directly

}
}

Is this wrong/bad design ?? If so, what is a better way to achieve
this ???
Jul 17 '05 #1
5 4314
devu wrote:

I therefore propose an inner class....

class Process {

doEligibility () {
EmpInfo a = new EmpInfo();
a.attrib1 = //set it
a.attrib2 = //set it

//store in hashmap with empID as key

}

private Class EmpInfo { //INNER CLASS
int attrib1;
String attrib2;
....
}

doProcess {

EmpInfo a = (EmpInfo) map.get("empID");

//use the attributes directly

}
}

Is this wrong/bad design ?? If so, what is a better way to achieve
this ???


If possible, I would suggest making the attributes of EmpInfo final.
When doing this, you must set them in the constructor. For example,

private class EmpInfo
{
public final int attrib1;
public final String attrib2;

public EmpInfo(int a1, String a2)
{
attrib1 = a1;
attrib2 = a2;
}
}

I assume your real classes will have better variable names. :)

This way you are not breaking anything since other classes will only
have read-only access to the attributes. The drawback is that they
cannot change after construction.

Also, there is probably no need to make EmpInfo an inner class, a nested
class will likely do. For example here are your choices:

// Start Main.java

public class Main
{
// Class Inner must be associated with an instance of class Main
// and has access to private things of class Main
private class Inner {}

// Class Nested need not be associated with an instance of Main
// and has access to private things of class Main
private static class Nested {}
}

// Class Another need not be associated with an instance of Main
// and has no special access to class Main
class Another {}

// End Main.java
HTH,
Ray
Jul 17 '05 #2
inner classes where designed for a reason, simplicity of code is one. if
reducing class names in your global namespace is of importance to you go
right ahead. sounds to me like you are after reading up on some GoF...

give it a whirl, you'll know soon enough

- perry
devu wrote:
Currently I'm working on a class that performs a batch process as per
scheduling for all employees in a company. Being a batch process load
is obviously a huge factor.
The process first needs to calculate the eligibility of each employee
before proceeding to the next stage. I need to cache some of the
employee information if an employee is found eligible for later use in
the next stage.
One option could be a javabean style value object class with all
required attributes and getter setter methods. However I would like to
avoid the overhead of these method calls for say 3000 eligible
employees. A lightweight object where I can access the attributes
directly would help me

I therefore propose an inner class....

class Process {

doEligibility () {
EmpInfo a = new EmpInfo();
a.attrib1 = //set it
a.attrib2 = //set it

//store in hashmap with empID as key

}

private Class EmpInfo { //INNER CLASS
int attrib1;
String attrib2;
....
}

doProcess {

EmpInfo a = (EmpInfo) map.get("empID");

//use the attributes directly

}
}

Is this wrong/bad design ?? If so, what is a better way to achieve
this ???


Jul 17 '05 #3
Raymond DeCampo <rd******@spam.twcny.spam.rr.spam.com.spam> wrote in message news:<JA*******************@twister.nyroc.rr.com>. ..
devu wrote:

I therefore propose an inner class....

class Process {

doEligibility () {
EmpInfo a = new EmpInfo();
a.attrib1 = //set it
a.attrib2 = //set it

//store in hashmap with empID as key

}

private Class EmpInfo { //INNER CLASS
int attrib1;
String attrib2;
....
}

doProcess {

EmpInfo a = (EmpInfo) map.get("empID");

//use the attributes directly

}
}

Is this wrong/bad design ?? If so, what is a better way to achieve
this ???
If possible, I would suggest making the attributes of EmpInfo final.
When doing this, you must set them in the constructor. For example,


Any particular design reason why it ought to be final ??? Since there
are about 10 attributes, that would be one hell of a constructor... :)
private class EmpInfo
{
public final int attrib1;
public final String attrib2;

public EmpInfo(int a1, String a2)
{
attrib1 = a1;
attrib2 = a2;
}
}

I assume your real classes will have better variable names. :)
They do.... :)
This way you are not breaking anything since other classes will only
have read-only access to the attributes. The drawback is that they
cannot change after construction.

Also, there is probably no need to make EmpInfo an inner class, a nested
class will likely do. For example here are your choices:

// Start Main.java

public class Main
{
// Class Inner must be associated with an instance of class Main
// and has access to private things of class Main
private class Inner {}
What advantage would making it static give me in particular
....especially if its private ???

// Class Nested need not be associated with an instance of Main
// and has access to private things of class Main
private static class Nested {}
}

// Class Another need not be associated with an instance of Main
// and has no special access to class Main
class Another {}

// End Main.java
HTH,
Ray

Jul 17 '05 #4
devu wrote:
Raymond DeCampo <rd******@spam.twcny.spam.rr.spam.com.spam> wrote in message news:<JA*******************@twister.nyroc.rr.com>. ..

If possible, I would suggest making the attributes of EmpInfo final.
When doing this, you must set them in the constructor. For example,
Any particular design reason why it ought to be final ??? Since there
are about 10 attributes, that would be one hell of a constructor... :)


It depends on how OO you want to be. The advantage is that clients of
the class can access the data without using a method. Encapsulation is
not broken since the values cannot be changed.

If the class is purely a collection of data, analogous to a struct in C,
you should be OK without the final. You might get into trouble if the
client should be accessing the data via a member but setting it via a
method...
private class EmpInfo
{
public final int attrib1;
public final String attrib2;

public EmpInfo(int a1, String a2)
{
attrib1 = a1;
attrib2 = a2;
}
}

I assume your real classes will have better variable names. :)

They do.... :)
This way you are not breaking anything since other classes will only
have read-only access to the attributes. The drawback is that they
cannot change after construction.

Also, there is probably no need to make EmpInfo an inner class, a nested
class will likely do. For example here are your choices:

// Start Main.java

public class Main
{
// Class Inner must be associated with an instance of class Main
// and has access to private things of class Main
private class Inner {}

What advantage would making it static give me in particular
...especially if its private ???


There are two reasons. First, if the contained class conceptually does
not need an instance of the containing class to exist your code reflects
that relationship. Second, when you make an inner class (i.e.,
non-static), the compiler adds a reference to the outer class to the
inner class. Also, the compiler may add accessors and/or mutators to
the outer class (although this may happen in the case of a nested class
as well). Remember that the JVM has no notion of inner or nested
classes and all the access they get are really compiler tricks.

Mostly I would worry about the conceptual reasons. What is the
relationship between the objects? Should each instance of the class be
associated with an instance of the containing class? For example, it
the containing class is a Customer and the contained class is an Order,
an inner class would be appropriate. OTOH, if the containing class is a
Parser and the contained class is a Token, a nested class would be more
appropriate.

Another reason is that if your outer class has static methods they will
be able to instantiate instances of the nested class.
// Class Nested need not be associated with an instance of Main
// and has access to private things of class Main
private static class Nested {}
}

// Class Another need not be associated with an instance of Main
// and has no special access to class Main
class Another {}

// End Main.java

The most important thing is to understand what you are doing and why you
are doing it. Don't just do something because someone on the Usenet
told you it was the best way. If you don't really understand why or how
it is best for your situation, it probably won't work out.


HTH,
Ray

Jul 17 '05 #5
On 29 May 2004 03:50:36 -0700, de*******@rediffmail.com (devu) wrote
(more or less):
Currently I'm working on a class that performs a batch process as per
scheduling for all employees in a company. Being a batch process load
is obviously a huge factor.
The process first needs to calculate the eligibility of each employee
before proceeding to the next stage. I need to cache some of the
employee information if an employee is found eligible for later use in
the next stage.
One option could be a javabean style value object class with all
required attributes and getter setter methods. However I would like to
avoid the overhead of these method calls for say 3000 eligible
employees. A lightweight object where I can access the attributes
directly would help me


When I did benchmarkinng in Smalltalk, direct access oy saved 3%
against using accessor methods.

Is using accessors in Java realy much more expensive? Or are you just
wanting to save the 3% for your 3000 instances?
--
Cheers,
Euan
Gawnsoft: http://www.gawnsoft.co.sr
Symbian/Epoc wiki: http://html.dnsalias.net:1122
Smalltalk links (harvested from comp.lang.smalltalk) http://html.dnsalias.net/gawnsoft/smalltalk
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Average_Joe | last post by:
Hello PHP people, Was wondering if PHP5 had some sort of "nested class" functionality. I'm trying to figure out how to return an object (with a private constructor) that has access to...
10
by: Paul Morrow | last post by:
I'm hoping that someone can explain why I get the following exception. When I execute the code... ###################################### class Parent(object): class Foo(object): baz = 'hello...
7
by: Wolfgang Jeltsch | last post by:
Hello, I want to write a list class with an iterator class as an inner class. The iterator class must have access to certain private members of the list class in order to do its job. Here is a...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
1
by: Mark Paskin | last post by:
Hello, I'm having difficulty getting template matching to work with inner classes. I've tried to look up the way to fix this in my references, but I haven't found the fix yet. When I try to...
6
by: Marco | last post by:
Howdy! Given: public abstract class A { public abstract int A1(int i); private class B { private int B1(int i) { int j;
9
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added...
6
by: Philip Potter | last post by:
I have a graph data structure. It goes something like this: class GraphNode { private: friend class Graph; // successor GraphNodes stored as indices of the Graph's array int value; int next1,...
9
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.