Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Java question

Is it possible to accomplish something like this in Java:

 

public class ModuleInformation
{

    public String _codname="";

    public int  _totalsize = 0;

    public int  _totalNumberOfSegments=0;

    public int  _currentNumberOfSegments=0;


    A(20)    
     {
       private byte[]  _segment = null;
       private int       _segmentSize = 0;
     }
}

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
rcmaniac25
Posts: 1,804
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.2.1.2174, Z10 (STL100-3)-10.3.0.700 Dev OS, Z30 (STA100-5)-10.3.0.700 Dev OS, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Java question

Not that I know of. Can you explain what "A(20)" means? Is it a class, a function (call), etc.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Java question

Just that I want

 

     {
       private byte[]  _segment = null;
       private int       _segmentSize = 0;
     }

 

to be an array of 20

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
YamilBracho
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Java question

Something like :

 

class Data {
      private byte[]  _segment = null;
       private int       _segmentSize = 0;

    // Constructor, getters and setters 
 }

Data [] myArray = new Data[20];
myArray[0] = new Data();
....
 

 

 

Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Java question

Well, maybe.

 

Combined with what I first wrote.  A class that holds both an array of integers

and an array of byte[]  as well as an non array integer

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Java question

Maybe something like this is what you have in mind:

 

public class ModuleInformation
{
    public String _codname = "";
    public int  _totalsize;
    public int  _totalNumberOfSegments;
    public int  _currentNumberOfSegments;
    private SegmentInformation[] A;

    // Default constructor allocates 20 SegmentInformation objects
    public ModuleInformation() {
        A = new SegmentInformation[20];
        for (int i = 0; i < A.length; ++i) {
            A[i] = new SegmentInformation();
        }
    }

    private static class SegmentInformation {
        byte[] _segment;
        int _segmentSize;
    }
}

Note that you don't need to initialize ints to 0 or object references to null; that's automatic. However, a String is an object reference as well, so if you want it initialized to "", it has to be explicit, either in the c'tor or in the field declaration.

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Java question

It sure looks OK. Being unacustomed with some of JAva I got a final question

 

Can I access the individual entries with getters and setters taking 2 parameters then

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: Java question

You can define 1- and 2-arg static methods:

 

public class ModuleInformation
{
    private int _codname = "";
    // ...
    public static String getCodName(ModuleInformation module) {
        return module._codname;
    }
    public static void setCodName(ModuleInformation module, String name) {
        module._codname = name;
    }
    // etc.
}

It's usual to declare the field private as I've shown to force client code to use the accessors.

 

Usually, though, getters and setters are defined as 0- and 1-arg member methods:

 

public class ModuleInformation
{
    private int _codname = "";
    // ...
    public String getCodName() {
        return _codname;
    }
    public void setCodName(String name) {
        _codname = name;
    }
    // etc.
}

Be aware that if you want client code to use SegmentInformation objects, the inner class cannot be declared private. (You might also consider making it a top-level class. The only reason for having static, non-private inner classes is to cut down on namespace pollution.)




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Developer
ingvar_e
Posts: 551
Registered: ‎10-31-2009
My Device: Torch 9800, Bold 9700
My Carrier: Movistar, Telenor

Re: Java question

Hmm,

 

What I meant was. I understand how I can get the name for example with get.The question was can I set and get the size o segment 3. 

 

I mean I can really solve my problem by having two classes.  One keeping the name and size of the total cod-file as well as how many segments there is  and then a class that holds the size and data of the segment. I would then instantiate the segment class as an array to keep the individual segment info.  Its not very elegant.  Most of my long time programming experience is with other languages where this is easily combined in a "struct"  to use "C" as an example.

If everything seems to be under control, you're just not driving fast enough
-Mario Andretti-
Please use plain text.
Developer
jMarc
Posts: 94
Registered: ‎10-01-2009
My Device: Not Specified

Re: Java question

That's exactly what Ted_Hopp did: two classes. The first one you describe is ModuleInformation, the second is SegmentInformation.  What's inelegant about it?

 

As for doing it with a struct, remember that a C++ struct is just a class with no methods and only public members. You can write a stripped-down class like that in Java, too, just don't tell the Javacops I'm the one who said so.  There's almost always a better way to do it.

 

But while we're on the subject... you know Java arrays are objects of a limited class-like entity, too: public (direct-access) elements, and a length field that reports the number of elements it can hold.  And a 2D array is an array of arrays, not necessarily all the same size.  If you're just storing the contents of a segmented file, and you know how many segments there are, what do you need a class for?

 

I don't want to completely destroy my credibility, but sometimes the simple way is the best way.

Please use plain text.