Declaration of Constants in classes

Hi experts,
Can we declare Constants in Abap classes as we delcare in report programming using Constants statement  and pass them ? Please let me know the procedure ?
Thanks and regards,
Uma.

Hello,
Yes, you can, if you are creating an global class go to transaction SE24 , under tab ATTRIBUTES define a name and define the level as CONSTANT  and the visibility like Public .
If you're using a local class, in the PUBLIC SECTION of the definition of the class define the constants using the command CONSTANTS .
Follow an example of local class with constants (defined in a report):
CLASS lcl_class DEFINITION.
  PUBLIC SECTION.
    CONSTANTS: lco_test TYPE string VALUE 'TEST'.
ENDCLASS.                    "lcl_class DEFINITION
DATA lv_test TYPE string.
lv_test = lcl_class=>lco_test.
Regards and reward if usefull,

Similar Messages

  • Comparison between constants in class and interface

    Hi,
    I have recently read an article which says it is a better use of your memory if you declare constants in an Interface rather than in a Class. Can any one explain this is more detail? Have a great day.
    Thank you,
    npaila

    @ricardo_moral
    Hm ...
    we are talking of constants in this topic - constants are always final, otherwise it would not be garanteed, that they cannot change. Also we are not talking of holding a copy of this constant field in a class, we are talking of directly referring to this static final field, regardless if it is declared in an interface or in a class.
    I prefer using an interface - my reason is that I can use it the same way, I can use a class, which declares this constant - via qualifier - AND I can implement it, if I want to - so, I follow the swing route and declare constants, which are shared by many classes, in an interface - but I implement it only there, where these constants are heavily used.
    greetings Marsian

  • Reason for not allowing static declarations inside an inner class

    Is the reason for not allowing static declarations inside an inner class is due to the fact that it can never be accessed at a class level as the outer class has to create an instance of the inner class and any attributes/methods of the inner class has to be accessed through that.
    Typically, an instance (non-static) variable can never be accessed in a statement or expression inside a static context but the class variable can be accessed inside a non-static context. Given this, shouldnt the static declarations be allowed inside an inner class?
    Correct me if my understanding is wrong.
    Thanks

    I still couldnt get it clearly. Why i cant i have a static value ( variable ) for all the instances of the inner class irrespective of its enclosing instances of it ( i.e outer class instances). Say in this example below,
    class Outer
    static int i = 0;
    public Inner inner = new Inner();
    class Inner // inner class ( non-static nested class )
    int j = 0;
    static final int k = 2; // compile time constants are allowed
    // ininner class
    public void m1()
    j++;
    System.out.println("j is " + j);
    i++
    System.out.println("i is " + i);
    public static void main(String[] arg)
    Outer outer1 = new Outer();
    outer1.inner.m1(); // j will be 1 & i will be 1
    Outer outer2 = new Outer();
    outer2.inner.m1() // j will be 1 again & i will be 2. But I would
    // want j to be 2. Why is this not allowed?
    Looks like something missing..

  • How to declare ftp in File class. I want to display filename of file in FTP

    How to declare ftp in File class. I want to display filename of file in FTP server.
    I try
    File f2= new File("ftp://abc:abc1111@ABC/file");
                   String n[] = f2.list();
                   System.out.print(n[0]);
                   Vector filename = null ;
                   for (int i2 = 0; i2 < n.length; i2++) {
                        filename.add(n[i2]);
    but it is not work
    Thank you.

    You can use Jakarta's net-package to connect to a FTP server.
    Download it here:
    http://jakarta.apache.org/site/downloads/downloads_commons-net.cgi
    Here's a small demo:
    import org.apache.commons.net.ftp.*;
    public class FTPTest {
        private boolean isConnected;
        private FTPClient client;
        public FTPTest() {
            isConnected = false;
            connect();
        private void connect() {
            String server = "server.org";
            String username = "user";
            String password = "password";
            client = new FTPClient();
            try {
                client.connect(server);
                client.login(username, password);
                String[] files = client.listNames();
                for(int i = 0; i < files.length; i++) {
                    System.out.println(files);
    isConnected = true;
    disconnect();
    catch(Exception e) {
    e.printStackTrace();
    private void disconnect() {
    if(isConnected) {
    try {
    client.disconnect();
    isConnected = false;
    catch(Exception e) {
    e.printStackTrace();
    public static void main(String[] args) {
    new FTPTest();

  • Syntax Error "Method was not declared or inherited in class"

    Dear all,
    I am seeing the following syntax error in one of the standard classes CL_ICCMP_BT_INRHISTRESULT_CN01 which says "Method "XYZ" was not declared or inherited in class". Need help to identify when this error would occur or where I should be checking for this error within the class.
    I checked other systems for the same class, the method XYZ does not exist there too but still the class has no syntax errors!
    Would appreciate your help!
    Thank you.
    Regards,
    Gayathri.

    Hi Gayathri,
    please give detailed information on how and where you get this error.
    CL_ICCMP_BT_INRHISTRESULT_CN01 does not exist in our ECC600 system.
    Regards,
    Clemens

  • How to declare interface within a class

    1. I want to declare interface within a class. How can i do that & whats the advantage ?
    2. How to declare class within a interface ?
    Can anybody tell me the concept ?/
    Thanks in advance
    madhu

    Classes and interfaces are types.
    What is the advantage of declaring a type within another one?
    Do you know what is the advantage of declaring a class within another class?

  • Function to read constants from class

    Hi all,
    i'd like to ask you if anybody konws a FM to read constants and their values form class?
    Thank you
    J

    Hi,
    i don't kown waht this FM does but it for sure doesn't import any constants from class.
    Thank you
    J

  • Purpose of declaring the method or class or static and as instance

    what is the purpose of declaring a method in a class or the class itself as static.
    Does it mean we cannot create a copy of that class or method if we declare it as static.
    if so then why do they dont want that class to be created as a copy ?
    Why do they want to declare a class as static
    please provide some conceptual undersatnding regarding the static and instance class with one example

    Static methods are often used for the implementation of utility methods. Please have a look at the class CL_ABAP_CHAR_UTILITIES for example.
    You use the methods of this class in the same way as you would use a function in ABAP (like
    LINES( itab )
    ). You use it in a static way because the functionality is always the same no matter in what context you are calling the function.
    The purpose of instance methods is that their logic is in some way related to an attribute of the object instance that you use to call it.
    For example, you create an instance of object PO (a purchase order) called MY_PO. Then the method
    MY_PO->ADD_POSITION
    would add a position to a concrete PO that has a unique number etc. But if the object has a static method DELETE_POSITION then it just deletes the current position of a PO, regardless on which concrete PO you are acting at the moment.
    I hope this clarifies it for you.
    Regards,
    Mark

  • When to declare instance of a class

    I am coding an address book with a GUI and I would like to separate the GUI and the Business logic. Where in my code should I declare an Instance of my Address Class so it can be used thought the code? For example in my business logic class I have a print method and a search method, in my GUI I have action a listener like this...
    Address cm = new Address();//where should this go?
    public class MainMenuHandler implements ActionListener
         public void actionPerformed(ActionEvent e)
                   if(source == filePrint)
                        cm.print();//want to call print here
    }//end MainMenuHandler and another one like this...
    public class ButtonHandler2 implements ActionListener
         public void actionPerformed(ActionEvent e)
              if(choice.equals("OK"))
                cm.add();//Is this even possible?     
         }//end actionPerformed
    }What would be the best way to do this? Create a new instance in each listener? Some kind of Global?
    Thanks for your help
    Rob

    What would be the best way to do this? Create a new
    instance in each listener? I don't think so. Where are you displaying the addresses in the book? The printing and other actions should reference the same place that the display comes from.
    I am assuming you are saving the addresses somewhere. When you load them (from a file, etc.) that is where you create them. Also, you would need to create a new one when a user adds an address.
    Some kind of Global? No.

  • Declaration of constant errors

    I'm having a bit of a problem here. I just changed my variables into constants, and I'm getting errors on it. I can't pin point the problem can someone help me out.
    public class MazeException extends Exception
        final String message;
        final Throwable cause;
        public MazeException(String message)
            super(message);
        public MazeException(String message, Throwable cause)
            super(message, cause);
    }

    They are not constants, they are final instance variables.
    To make them into constants add the static modifier. To make them accessible to other classes, if you need to, add the public modifier as well;
    public static final String message = "Some text in here";Then, you can refer to them within the class using their simple name, from outside the class with their fully qualified name.
    Also, I cannot see anywhere where the constants are initialised. All you constructors seem to do is pass their parameter values on up to the matching constructor in the superclass.

  • Declaration method in users Class

    What's different between declaration method in these codes
    class Programm
    ClassA class1 = new ClassA();
    class1.TestMethod();
    public ClassA
    public void TestMethod()
    and
    class Programm
    ClassA.TestMethod();
    public ClassA
    internal static void TestMethod()

    There are 2 main differences.
    Your access modifier is different (public / internal). See this link for information about the 4 different access modifier. https://msdn.microsoft.com/en-us/library/ms173121.aspx
    Your first method has instance scope and the second one has a static/global scope. See this link on what the difference is between the 2. https://msdn.microsoft.com/en-us/library/aa645629(v=vs.71).aspx
    -Igor

  • How to declare a custome AS-class that extend a Flex componenet

    Hello there, would you please help. I have the following mxml tag
    <mx:Application xmlns:mx="xmlns:fx="http://ns.adobe.com/mxml/2009"xmlns:custom="com.myComponents.*" layout="absolute" />
    <custom:ReturnData id="dataR" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true" />
    <fx:Declarations>
    <s:CallResponder id="getAllResult"/>
    </fx:Declarations>
    When I change this into AS and try to use the result in a dataGrid, no info will be populated in datagrid, any idea??? It works when use the xmlns tag.
    2) as actionscript object
    <fx:Script>
         <![CDATA[
              import com.myComponents.*;
              public var dataR:ReturnData;
              getAllResult.token = dataR.getAll();
         ]]>
    </fx:Script>
    <mx:DataGrid x="10" y="21" id="dataGrid" creationComplete="dataGrid_creationCompleteHandler(event)" dataProvider="{getAllResult.lastResult}">
    <mx:columns>
    <mx:DataGridColumn headerText="TreatmentareaID" dataField="TreatmentareaID"/>
    <mx:DataGridColumn headerText="ProductName" dataField="ProductName"/>
    </mx:columns></mx:DataGrid>I

    Thanks for your feedback, my original code is as here below. DataGrid2 populate the data since the component has been initialized in the xmlns. DataGrid does not populate the data and has been initialized by ActionScript???
    Any idea where do I do wrong?
    Thanks
    <?xml version="1.0" encoding="utf-8"?>
    <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
       xmlns:s="library://ns.adobe.com/flex/spark"
       xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" initialize="application1_initializeHandler(event)" minHeight="600"  xmlns:productrestylaneservice="services.productrestylaneservice.*">
    <fx:Script>
    <![CDATA[
    import mx.controls.Alert;
    import mx.events.FlexEvent;
    import services.productrestylaneservice.*;
    [Bindalbe]public var productRestylaneService:ProductRestylaneService;
    protected function dataGrid_creationCompleteHandler(event:FlexEvent):void
    getAllTreatmentAreaRestylaneResult.token = productRestylaneService.getAllTreatmentAreaRestylane();
    protected function application1_initializeHandler(event:FlexEvent):void
    getAllTreatmentAreaRestylaneResult.token = productRestylaneService.getAllTreatmentAreaRestylane();
    protected function dataGrid2_creationCompleteHandler(event:FlexEvent):void
    getAllTreatmentAreaRestylaneResult2.token = productRestylaneService2.getAllTreatmentAreaRestylane();
    ]]>
    </fx:Script>
    <fx:Declarations>
    <s:CallResponder id="getAllTreatmentAreaRestylaneResult"/>
    <s:CallResponder id="getAllTreatmentAreaRestylaneResult2"/>
    <productrestylaneservice:ProductRestylaneService id="productRestylaneService2" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    <mx:DataGrid x="10" y="21" id="dataGrid" creationComplete="dataGrid_creationCompleteHandler(event)" dataProvider="{getAllTreatmentAreaRestylaneResult.lastResult}">
    <mx:columns>
    <mx:DataGridColumn headerText="TreatmentareaID" dataField="TreatmentareaID"/>
    <mx:DataGridColumn headerText="ProductName" dataField="ProductName"/>
    </mx:columns>
    </mx:DataGrid>
    <mx:DataGrid x="462" y="21" id="dataGrid2" creationComplete="dataGrid2_creationCompleteHandler(event)" dataProvider="{getAllTreatmentAreaRestylaneResult2.lastResult}">
    <mx:columns>
    <mx:DataGridColumn headerText="TreatmentareaID" dataField="TreatmentareaID"/>
    <mx:DataGridColumn headerText="ProductName" dataField="ProductName"/>
    </mx:columns>
    </mx:DataGrid>
    </s:Application>

  • Managing constants in base / derived classes

    Hi there,
    The problem:
    I'm facing what is certainly a simple problem but fail to come up with an acceptable solution. What I'd like to do is managing constants in a base class and derived class... which for some reasons are not parts of the same package. Also, these constants may have different values in the derived base.
    Down to earth description:
    Class base uses a set of constants A=1, B=2, C=3.
    Class derived should be provided with similarly named constants but they may have different values A=10, B=20, C=30. I could declared these constants in the classes themselves but somehow I think this is ugly.
    A "solution" that does not work:
    Assume for a moment that I'm just a very naive programmer (which actually I am... but anyway...). I would them come up with something like:
    package base;
    import base.Constants;
    public class Base {
        public Base() {
        public void myfunction() {
           System.out.println("My constant: " + Constants.A);
    }and
    package derived;
    import derived.Constants;
    public class Derived extends Base {
        public Derived() {
    }Now of course this is stupid since Derived.myfunction() prints the value of the constants given by Base.myfunction()The question is: is there a static construction that would allow me to have the desired behaviour? What I mean is that I don't want to provide a "Constants" object nor an hashtable dynamically loaded or whatever.
    Bonus question:
    I'm not a pro in OOP (seriously guys... C rocks! Oops, wrong forum :-) but it seems to be a case of "code inheritance" vs "behaviour inheritance" (I just made those expressions up!). What I mean is: if what is inherited is the actual "code" of the methods (as if a huge copy paste took place) then the naive solution would work. But in the real world, a "behaviour inheritance" is at work in the sense that what is inherited is the "behaviour" (or, more bluntly, simply the code as compiled in the base class). Hmm... Am I making any sense here? Is that distinction theorized in some way? I tried to search a bit by myself but did not know where to start.
    Thanks,

    I mucked around with this exact problem a while back.
    The (simplified) scenario is AbstractCourse is extended by CookingCourse, WritingCourse, and BusinessCourse. Every course has a basePrice and a materialsCost... both of which vary from course to course... The same price calculation applies to all types of course, just the amounts will vary.
    totalPrice = basePrice + materialsCost The traditional non-oo solution is simple enough... you would just create a "table" of all six contstants, and a function with a switch statement...
    but How to do this "The OO Way"?
    What I came up with is:
    class BaseCourse {
      abstract double getBasePrice();
      abstract double getMaterialsCost();
      public double getPrice() {
        return getBasePrice() + getMaterialsCost();
    public class CookingCourse {
      private static final double BASE_PRICE = 300.00;
      private static final double MATERIALS_COST = 112.60;
      public double getBasePrice() { return CookingCourse.BASE_PRICE }
      public double getMaterialsCost() { return CookingCourse.MATERIALS_COST }
    public class WritingCourse {
      private static final double BASE_PRICE = 200.00;
      private static final double MATERIALS_COST = 50.00;
      public double getBasePrice() { return WritingCourse.BASE_PRICE }
      public double getMaterialsCost() { return WritingCourse.MATERIALS_COST }
    public class BusinessCourse {
      private static final double BASE_PRICE = 600.00;
      private static final double MATERIALS_COST = 18.12;
      public double getBasePrice() { return BusinessCourse.BASE_PRICE }
      public double getMaterialsCost() { return BusinessCourse.MATERIALS_COST }
    }The base class specifies that each subclass must be able to tell it's base price, and it's materials cost... then we implement the calculation on those values just once, in the base class.
    For all I know there are much more succinct, flexible, efficient, and basically much more smarter ways of doing this... this is just a way that worked for me... and it's simple enough so even I can follow it.
    Java can be a very very very very verbose language :-( ... But hey it still &#115;hits on C ;-)
    Cheers. Keith.
    Edited by: corlettk on 23/05/2008 11:17 - typos

  • Constant declaration

    I am trying to declare a constant. I keep getting an "illegal start of expression error" on the lines that declare my constants. I have tried changing "public" to "private" and I have also tried leaving the first modifier off entirely, but it still gives me the error. Can anyone tell what I am doing wrong here?
    class Example2 {
          public static void main(String [] args) {
          public static final int DODGE = 1;
          public static final int FORD = 2;  
          int vehicle;
          vechicle = FORD;
          switch(vehicle) {
             case FORD:   System.out.println("You chose Ford");
                                      break;
             case DODGE:   System.out.println("You chose Dodge");
                                         break;
    }

    I am trying to declare a constant. I keep getting an
    "illegal start of expression error" on the lines that
    declare my constants. I have tried changing "public"
    to "private" and I have also tried leaving the first
    modifier off entirely, but it still gives me the
    error. Can anyone tell what I am doing wrong here?
    class Example2 {
    public static void main(String [] args) {
    public static final int DODGE = 1;
    public static final int FORD = 2;  
    int vehicle;
    vechicle = FORD;
    switch(vehicle) {
    case FORD:   System.out.println("You chose
    Ford");
    break;
    .println("You chose Dodge");
    break;you could try 'enum' , this keyword exists since 1.5
    sorry for late response
    Message was edited by:
    p_epi

  • Declaring a class..........

    Hi all,
    Can you please tell me , in a file why it is not allowed to declare more than one class as public and also the file name should be the name of the public class?
    Thanks in advance,
    Regards,
    Devi

    1) the compiler forces you to do so.
    2) it would be a configuration nightmare if it were not so
    3) every class defined in a source file corresponds to a class file
    4) if you had a compiler error, or an exception, or a classpath
    problem, then where would you look for the source code involved?
    5) it's true that the class file contains a constants pool and line number
    table and the source file name is in there. but why make things so
    complicated when it's easy to have the source file and class file the same
    name?

Maybe you are looking for

  • How can I "profile" folio articles load time and memory use?

    Hi folks, I am beating my head against the wall since last week: How can I figure out a problematic article (one or more), that causes both Content Viewer and custom viewer (.IPA) to crash on load? The crash report states that "application took too l

  • How to Activate Viber on iPhone and iPad

    Hi, I try download Viber to iPhone and my iPad mini. It work only on 1 device. Every time when is Viber activated on iPhone and I try to do it on iPad iPhone automaticly delete activation on and then Viber work only on iPad. If I will try to activate

  • Can´t read page containing XML using GET HTTP Document

    Hello forum users,  I'm have a problem with the LabVIEW Internet Toolkit and I'm woundering if its me that is doing something wrong or if it is a limitation of the VI.  I have a robotic application which comunicates with LabVIEW via internet.  the ro

  • BUG in Intermedia

    Intermedia cannot handle wildcards in large set of records First set of cases is OK , since the data is less . Second set of cases fail because of the large data . select count(*) from S37PORTD.cmdoccontents - 933 select count(*) from S37PORTD.cmdocc

  • Procurement Cycle of material type "PIPE"

    Hi Gurus , Can you please explain the purchase Cycle (Order to Invoicing Cycle ) of the material type "PIPE".I belive that Purchase Orders are not created for the same. Thanks, Tausif