I am fairly new to java and I have a program that I really need to get working. I need to pass 'alarmId' from class A to class B. I am not too sure how to go about this, any help would be greatly appreciated.

public void onStart()
{
  
}
public void onExecute() throws Exception
{  

String newValue = getValue().valueToString(null);
}  
  public class A{
  
                
  if(!newValue.equals("P Normal"))
  { 
    BAlarmService service = (BAlarmService)Sys.getService(BAlarmService.TYPE);
    BAlarmRecord record = new BAlarmRecord(); 
    record.setTimestamp(BAbsTime.now());
    record.setSourceState(BSourceState.offnormal);
    record.setAckState(BAckState.unacked);
    record.setAckRequired(false);
    record.setSource(BOrdList.make(getProgram().getSlotPathOrd()));
    record.setAlarmClass(service.getDefaultAlarmClass().getName());
    record.setAlarmData(BFacets.make(BAlarmRecord.OFFNORMAL_VALUE, newValue)); 
    record.setAlarmData(BFacets.make(BAlarmRecord.MSG_TEXT, newValue));
    record.setLastUpdate(BAbsTime.now());    
    service.routeAlarm(record); 
    String uuidStr = record.getUuid().encodeToString();
    BUuid alarmId = (BUuid)BUuid.DEFAULT.decodeFromString(uuidStr);
    }
}    
  
  

   public class B{
  if(newValue.equals("P Normal"))
  {         
    int alarmId = A.getalarmId();             
    BAlarmService alarmService = (BAlarmService)Sys.getService(BAlarmService.TYPE);
    BAlarmDatabase alarmDb = alarmService.getAlarmDb();
    BAlarmRecord record1 = alarmDb.getRecord(alarmId);
    record1.ackAlarm();
    alarmDb.update(record1);  
    alarmService.ackAlarm(record1);
    }
  }
}

Those code fragments don't make any sense strung together like that - I assume you have just posted the bits you think are relevant.
Anyway, B has
int alarmId = A.getalarmId();
so class A must have a getalarmId() method, and that's how 'alarmId' gets passed from class A to class B.

to expend on what james said :

if the A class is only ever gonna have 1 alarm ID at a time, you are indeed able to create a static method "getAlarmId()"
it needs to be static to be called from the Class name itself

Class A{
    private int alarmID;
    public static int getAlarmId(){
         return alarmID;
    }
}
Class B{
    int alarmID = A.getAlarmId() 
}

otherwise if multiple instances of class A are initialized in your program and they can contain diferent alarmId then the method cannot be static and will need to be called from a Class A OBJECT which Class B could receive from its constructor or through a method

Class B{
    int alarmID;
    public new B(A myClassAObject){
         alarmID = myClassAObject.getAlarmId();
    }
}

or

Class B{
    int alarmID;
    public new B(){
    }

    public void setAlarm(A myClassAObject){
         alarmID = myClassAObject.getAlarmID(); 
         //assuming that class A has alarmID set as private 
         //otherwise myClassAObject.alarmID; would be good enough
    }
}

in both cases you need to send the whole instance of your Class A object to the class B object from your main or where you need to retrieve the alarmId.

Edit:
be sure to try and respect standards in situations where lets say Class A has Class B objects within it then the Class B objects cannot logicly call Class A methods because it should not talk to its parent directly, instead somewhere in Class A there should be a way to let the Class B object know what it needs to know and not the other way arround.

Yes Philippe.
I too was worried about the use of a static reference for the method call, but without seeing the rest of the code then maybe there's a good reason, so I ignored it for now. I would be surprised if static was a good choice, so your recommended approach is almost certainly the right one.

Well I changed my code to what I thought should work but I can not get it to compile.

public void onStart()
{
  
}
public void onExecute() throws Exception
{  

String newValue = getValue().valueToString(null);

  
   
     
   class A{
  if(!newValue.equals("P Normal"))
  {
    
    BAlarmService service = (BAlarmService)Sys.getService(BAlarmService.TYPE);
    BAlarmRecord record = new BAlarmRecord(); 
    record.setTimestamp(BAbsTime.now());
    record.setSourceState(BSourceState.offnormal);
    record.setAckState(BAckState.unacked);
    record.setAckRequired(false);
    record.setSource(BOrdList.make(getProgram().getSlotPathOrd()));
    record.setAlarmClass(service.getDefaultAlarmClass().getName());
    record.setAlarmData(BFacets.make(BAlarmRecord.OFFNORMAL_VALUE, newValue)); 
    record.setAlarmData(BFacets.make(BAlarmRecord.MSG_TEXT, newValue));
    record.setLastUpdate(BAbsTime.now());    
    service.routeAlarm(record); 
    String UuidStr = record.getUuid().encodeToString();
    BUuid alarmId = (BUuid)BUuid.DEFAULT.decodeFromString(UuidStr);
     
    public int alarmID;
    public static int getAlarmId(){
    return alarmId;}             
    }
   } 
     
    
    
        
  
                                                                                      
                                                            
   class B{
  if(newValue.equals("P Normal"))
  {         
    int alarmID;
    public new B(A myClassAObject){
    alarmId = myClassAObject.getAlarmId();
    }            
    BAlarmService alarmService = (BAlarmService)Sys.getService(BAlarmService.TYPE);
    BAlarmDatabase alarmDb = alarmService.getAlarmDb();
    BAlarmRecord record1 = alarmDb.getRecord(alarmId);
    record1.ackAlarm();
    alarmDb.update(record1);  
    alarmService.ackAlarm(record1);
    }
     }
}

I get a syntax error after the bracket on class A saying "}" inserted to complete class body.

I get a syntax error at int in class A saying: @ expected instead of this token.

I get a syntax error on this code snippet "public static int getAlarmId(){" that says: "Type VariableDeclarators" inserted to complete LocalVariableDeclaration

I get a syntax error on this code snippet in class A "public static int getAlarmId(){" that says: ";" inserted to complete BlockStatement.

class A{
   if(!newValue.equals("P Normal"))

You can't just start putting code into a class like that. Ithas to be in a method.

also both those classes are defined in "onExecute" which is where? please clarify because it cannot just be hanging out there on its own just like james said that your "if" statement can't be hanging arround on its own inside the A class.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.