Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


structure/package data types..

Posted on 2005-04-13
Medium Priority
Last Modified: 2013-12-03

working on java 1.5 for windows platform.

I have a base class CMainBase and have 4 classes derived from it , cboolean,

cintvalue, cstring and cother.

every data type(boolean, intvalue, string, other) has some attribute(s ) associated with it.


these attribute, can be any or combination for each datatype (string,

boolean, other, value). The length of each attribute is 1 byte.

I am planning to make a structure containing alll these types like:

public abstract class CMainBase {

class attrib{
byte read-only;
byte read-write;
byte execute-only;
byte hidden;

and then use the same. Is this the right way to proceed ?? How to use this

class to package all the attribtutes so that overall length should not exceed 1 byte ?? i mean to say looking at the value i can deduce the attributes present for a datatype.

All suggestion are welcome.  


Question by:rohitdivas
  • 3
LVL 15

Expert Comment

ID: 13774048
You can have one int variable (e.g int attributes)
and then have constants like:
public int READ = 1;
public int WRITE = 2;
public int EXECUTE = 4;

(each value is bigger then its previous one by multiply of 2).
Then you can assing to a type something like this:

intvalue intVal = ..
intVal.setAttributes( READ | WRITE); // or READ + WRITE;
which means that this value has RW attibutes.
If you want to check if this value has WRITE persmissions then you can do something like this:

if ((intVal & WRITE) > 0) ... // then this value has write persmissions.
There are many examples of the JDK for such technique (e.g. http://www.docjar.org/html/api/java/awt/Font.java.html)
LVL 15

Expert Comment

ID: 13774085
For the Font class it is they way its store its style atributes (PLAIN , BOLD, ITALIC)
or FlowLayout with its alignment attributes: http://www.docjar.org/html/api/java/awt/FlowLayout.java.html

Author Comment

ID: 13779109
The combination of all of them should not exceed 1 byte.  but here it is exceeding.

i need to achieve the functionality provided by enum data type (and enum is not provided in java) .  The method suggested by you will work, but  i am in search of alternatives methods.

LVL 15

Accepted Solution

aozarov earned 600 total points
ID: 13781044
You can still use byte and apply the same technique (as long as you have less then 8 combinations).
public byte READ = (byte) 1;
public byte WRITE = (byte) 2;
public byte EXECUTE = (byte) 4;
public byte HIDDEN = (byte) 8;

There is enum in Java 5.0 http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html
Though they will not be one byte only (they are treated as objects).


Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question