Regarding static or instance

Hi all,
           I am in confusion that which is called first static (method / attribute ) or Instance (method/attribute).
Becoz what i know is whenever an object is created, first static is called then instance.
please correct me if i am wrong.
regards,
satish

Hi Sathis,
Instance & Static Method :
if u declare one method as a static then we can call that method using class name, that method is independent of that object.You declare them using the CLASS-DATA statement.
if u declare one method as a instance then we can call that method using object name, that method is dependent of that object.You declare them using the DATA statement.
Instance & Static Attribute :
if u declare one attribute as a static then we can use that attribute through class name, that attribute is independent of that object.You declare static methods using the CLASS-METHODS statement.
if u declare one attribute as a instance then we can use that attribute through object name, that attribute is dependent of that object.You declare instance methods using the METHODS statement.
if u have any doubt ask me,
Regards,
Vijay
Edited by: Vijay.V on Dec 26, 2007 3:33 PM
Edited by: Vijay.V on Dec 26, 2007 3:36 PM

Similar Messages

  • Set static or instance on level of methods in class?

    Hi,
      Can anyone tell me to set the level of method in class, which is better, static or instance?Because if set static, I don't need to create instance for a class, but I don't know it's advantage and disadvantage.

    Hi,
    There is no rule, it depends on your requirement. Usually you create STATIC methods for UTILITY classes like CL_GUI_FRONTEND_SERVICES or MATHS functions.
    If its dependent on a business object, then it makes sense for a instance method, as its dependent on the object it does not make sense that to be static.
    Regards,
    Ravi
    Note : Please mark the helpful answers

  • Public static BookDB instance() - java book store

    hi, can some one explain what is the use of instance function. ( the code is from sun's Duke bookstore example )
    public static BookDB instance () {
    if (onlyInstance == null)
    onlyInstance = new BookDB();
    return onlyInstance;
    thanks..

    hello,baalaji
    This is a "singleton" design pattern.
    This pattern will make sure there is no more than one instance of BookDB class in the JVM.
    I am sure that you do not display another important phrase of codes---construction method. Have a look at this method, there must be:private BookDB(){
    } This private construction method makes sure no one could new an instance of this class through a "new" keywords. The only method can new an instance is through BookDBinstance()!
    Good luck and enjoy Java!
    Wang Yu
    Developer Technical Support
    Sun Microsystems
    http://sun.com/developers/support

  • Static or Instance methods

    Hi,
    I am a self-taught Java programmer and have been writing Java code over the past 18 months. I am comfortable with general coding, but I am having great difficulty deciding whether a method should be static or instance.
    In many cases, I decide to create a class which has maybe 1 or 2 methods. A good example of this is the class I wrote which allows me to pass it a JFrame and it then centres the JFrame on screen. Seems to me I can do 1 of 2 things.
    1. Put the code in the class constructor and then call it with:
    CentreMe cm = new CentreMe(this);
    where CentreMe is the name of the class and this refers to the JFrame that is being passed
    2. Put the code in a static method within the class and then call it with:
    CentreMe.CenterFrame(this);
    Assuming that all the CentreMe class does is to centre a frame in this way, which is the correct (or at least best) approach. Seems to me there is no point in creating objects as per option 1 when they are never used therafter?
    If anyone has advice or a URL that deals with this particular "design" issue I would be very grateful.
    Thanks
    Mark

    Given that design, then your intuition is correct, that method should be static. No need for objects there. But on a higher level, the design is backwards. Instead of having a separate class that knows how to centre a JFrame, it would be better to create a subclass of JFrame that knows how to centre itself. Have a look at Bruce Eckel's online book "Thinking in Java" ( http://www.bruceeckel.com ); it has a lot of useful information about the design of objects.

  • Name space conflict between static and instance method

    Hello,
    there seems to be a very unfortunate name space conflict between static and instance method name. Consider:
    static String description() that should return a description of a class.
    String description() that should return a description of an instance.
    That seems to confuse the compiler. How come? Static and instance methods don't have anything to get confused about.
    Thanks for any insights :-)

    Umm...jeez.
    It's not a bug, it's the way it's supposed to be.
    Since a static method can be called the same way an instance method
    A instance = new A();
    A.staticMethod();
    instance.staticMethod();it's not allowed.
    Also in the class, you can call
    public void myMethod() {
          instanceMethodInClass();        // You don't need this, it's superfluous
          staticMethodInClass();          // You don't need the class name.
    }If you didn't understand, then just accept the fact that it is so. Some day you'll understand it.

  • Convertion of class variable (static) into instance variable(non-static)!

    Dear all,
    got a slight different question.
    Is that possible to convert class variable (static) into instance variable(non-static)?
    If so, how to make the conversion?
    Appreciating your replies :)
    Take care all,
    Leslie V
    http://www.googlestepper.blogspot.com
    http://www.scrollnroll.blogspot.com

    JavaDriver wrote:
    Anything TBD w.r.to pass by value/reference (without removing 'static' keyword)?Besides the use of acronyms in ways that don't make much sense there are two other large problems with this "sentence".
    1) Java NEVER passes by reference. ALWAYS pass by value.
    2) How parameters are passed has exactly zero to do with static.
    Which all means you have a fundamentally broken understanding of how Java works at all.
    Am I asking something apart from this?
    Thanks for your reply!
    Leslie VWhat you're asking is where to find the tutorials because you're lost. Okay. Here you go [http://java.sun.com/docs/books/tutorial/java/index.html]
    And also for the love of god read this [http://www.javaranch.com/campfire/StoryPassBy.jsp] There is NO excuse for not knowing pass-by in Java in this day and age other than sheer laziness.

  • Can one combine static and instance methods?

    Hi,
    Can one define a method so that is can be used as both a static or an instance method?
    Basically I'm trying to simplify my class to so that I can call a method either statically with parameters or instantiated using it's own attributes.
    In other words, I'm trying to accomplish both of these with one method:
    zcl_myclass=>do_something_static( im_key = '1234' ).
    lv_myclass_instance->do_something( ).   " key in private attributes
    Why would I want to do that?
    I would like to avoid instantiation in some cases for performance reasons and would like to keep it simple by having only one method to do basically the same thing.
    Any input or alternative suggestions welcome.
    Cheers,
    Mike

    Ok, I may be reaching here a bit, I know, but maybe this may give you some ideas.  After creating the object, pass it back to the method call.
    report zrich_0001.
    *       CLASS lcl_app DEFINITION
    class lcl_app definition.
      public section.
        data: a_attribute type string.
        class-methods: do_something importing im_str type string
                                   im_ref type ref to lcl_app optional.
    endclass.
    *       CLASS lcl_app IMPLEMENTATION
    class lcl_app implementation.
      method do_something.
        if not im_ref is initial.
           im_ref->a_attribute = im_str.
          write:/ 'Do_Something - ',  im_ref->a_attribute.
        else.
          write:/ 'Do_Something - ',  im_str.
        endif.
      endmethod.
    endclass.
    data: o_app type ref to lcl_app.
    start-of-selection.
      create object o_app.
      call method o_app->do_something(
               exporting
                   im_str = 'Instansiated'
                   im_ref = o_app ).
      call method lcl_app=>do_something(
               exporting
                   im_str = 'Static' ).
    Regards,
    Rich Heilman

  • Need some explanations regarding static Method

    Hello,
    I have a code where a class has a static method access by other classes. This not working as I thought and I would like to understand why.
    Here below is the code.
    Here is a class having the static method "test"
    package test;
    public class StaticTest {
        public StaticTest() {
        public static void test (int i){
        while (i < 1000){
            System.out.println("i = " + i );
            i++;
    }Here is the code of another class (Thread) using this static method. This Thread can be initialized with an integrer value that will be passed to the static Method.
    package test;
    public class ThreadTester extends Thread {
        int x;
        public ThreadTester(int i) {
            x=i;
        public void run(){
            StaticTest.test(x);
    }Here is the code starting 2 Thread running in parallel and both are then calling the static method.
    //start 2 thread accessing the static method.
          ThreadTester test1 = new ThreadTester(0);
          ThreadTester test2 = new ThreadTester(200);
          test1.start();
          test2.start();
    ...As the second thread is started with a bigger value I thought that we would only have seen few printouts from the first thread starting by 0 and then printouts starting by 200.
    Here is what I thought regarding the result:
    i = 0
    i = 1
    i = 2
    i = 3
    i = 200 --> startup of the second thread, x in static method is overriden (at least this is what I thought!)
    i = 201
    i = 202
    i = 203
    i = 204
    i = 205
    i = 206
    i = 207
    i = 208
    i = 209
    But the real result is:
    i = 0
    i = 1
    i = 2
    i = 3
    i = 4
    i = 5
    i = 200
    i = 6
    i = 201
    i = 202
    i = 203
    i = 204
    i = 205
    i = 206
    i = 7
    i = 207
    i = 208
    i = 209
    i = 8
    It seems that there is 2 instances running in parallel. I thought that it would'nt be the case with the word "Static".
    Also I don't understand the result because if I use JBuilder in Optimizer mode I can see that there is only one instance of StaticTest object.
    Can anyone here explain me how is that possible?
    Thanks in advance for your help.
    Regards,
    Alain.

    >
    thread test1 creates its own stack and starts incrementing �i� starting at values 0. However, in the middle of incrementing, it gets kicked out by the OS (or JVM) into a �blocked� state to allow other threads to run. BUT before leaving the running state, test1 saves the stack state including the value of �i�.
    >
    Ok, now I understand, but then I have another question.
    What is the difference between the code shown in my first post and the following where we create 2 instances of StaticTest class (which is not static in this case for sure).
    How to decide (while coding) if it is better to use difference instances of class or not?
    package test;
    public class StaticTest {
        public StaticTest() {
        public void test (int i){ //Not static anymore
        while (i < 1000){
            System.out.println("i = " + i );
            i++;
    }We create new instance in the Thread.
    package test;
    public class ThreadTester extends Thread {
        int x;
        public ThreadTester(int i) {
            x=i;
        public void run(){
    StaticTest newInstance = new StaticTest(); //Create a new instance
            newInstance .test(x);
    }Alain

  • Regarding static control

    Hi Experts,
                       i have knowledge on flow_conrol,but in which case i will go for static_control, please guide me
    Regards
    ksbabu

    Hi,
    You can use it in two different places :
    In the Model : for instance double-click your datastore and click "Datastore Static Control". It will perform a check on the data using all the constraints (keys, references and conditions) defined and load all the erroneous rows in an error table.
    In an Interface : If you set static control, it will perform the same operation AFTER the loadind of the target table. So all rows will be loaded into the target and then the static control will copy the erroneous rows in the error table.
    Hope it helps.
    Regards,
    JeromeFr
    ps: you should mark your discussions as questions when you create them

  • Regarding static methos and attributes...

    Hi..
    What do u mena by static methids and atributes?
    Why v'll use these?
    Whats the purpose?
    Regards
    sandeep.

    Hi,
    Static Attributes
    The contents of static attributes define the state of the class that is valid for all instances of the class. Static attributes exist once for each class. You declare them using the CLASS-DATA statement. They are accessible for the entire runtime of the class.
    All of the objects in a class can access its static attributes. If you change a static attribute in an object, the change is visible in all other objects in the class.
    The technical properties of instance attributes belong to the static properties of a class. It is therefore possible to refer in a LIKE addition to the visible attributes of a class – through the class component selector or through reference variables, without prior creation of an object.
    Methods
    Methods are internal procedures in a class that define the behavior of an object. They can access all of the attributes of a class. This allows them to change the data content of an object. They also have a parameter interface, with which users can supply them with values when calling them, and receive values back from them The private attributes of a class can only be changed by methods in the same class.
    The definition and parameter interface of a method is similar to that of function modules. You define a method meth in the definition part of a class and implement it in the implementation part using the following processing block:
    Static Methods
    You declare static methods using the CLASS-METHODSstatement. They can only access static attributes and trigger static events.
                Reward If Helpfull.
    Regards Madhu.

  • Static context error, nothing declared static, new instance isn't working

    I'm trying to get the IP address of a user using my applet. Nothing in my code is declared static.
    InetAddress IP = InetAddress.getAddress();I get non-static method getAddress cannot be referenced from a static context.
    I just read on another post with someone having a similar but not identical problem and someone replied saying you need to create a new instance. So I tried this:
    InetAddress IP = new InetAddress();
    //IP.getAddress();With this, I get the error: InetAddress(); is not public in java.net.InetAddress; cannot be accessed from an outside package
    What can I do? It's probably something simple.
    If you need code just ask, there's just alot of code and it might take awhile to recreate it.

    I'm trying to get the IP address of a user using my
    applet. Nothing in my code is declared static.
    InetAddress IP = InetAddress.getAddress();I get non-static method getAddress cannot be
    referenced from a static context.
    I just read on another post with someone having a
    similar but not identical problem and someone replied
    saying you need to create a new instance. So I tried
    this:
    InetAddress IP = new InetAddress();
    //IP.getAddress();With this, I get the error: InetAddress(); is not
    public in java.net.InetAddress; cannot be accessed
    from an outside package
    What can I do? It's probably something simple.
    If you need code just ask, there's just alot of code
    and it might take awhile to recreate it.In your first try the method you attempted to use can only be used in an instant of an InetAddress ie. ip.getAddress(). Therefore the compiler thought you were trying to call a static method that was really an instance method. On your second try you used the contructor of InetAddress which isn't public, so you can't use it. To make an InetAddress use any of the static methods of the InetAddress, which can be found at http://java.sun.com/j2se/1.4.1/docs/api/java/net/InetAddress.html

  • Regarding Static Price Allocation

    Hi All
    Thanks in Advance
    Can any one please tell me what in " Static Price Allocation " & how it is executed in R/3?
    Its Very Urgent
    Thanks & Regards
    Natash Singh

    Heard of Dynamic Price Changes, but not Static Price Allocation...
    Can you provide the context please?
    Cheers.

  • Regarding static

    Hi,
    may i know how it works if v use static accoring memory and while linking the class file. i know according to memory, i need to know while linking, is it same for c and c++, how it differs?.
    plz send me
    siva

    If we declare a variable 'static' , it is available for all instances of the class .

  • Confused regarding static methods

    Hello guys,
    I like to know the only purpose of static method is to call a method with out creating
    instance of that class.
    I am totaly confused why and where to use static methods.
    Thanks in advance
    aneesh

    Look at the API docs for some of the static methods in classes like java.lang.String, java.lang.Integer, java.lang.Thread, etc.
    An object--an instance of a class--has state and behavior. The state is captured in the instance variables--the array that holds the characters of a String, the name, SS#, and hireDate fields of an Employee object, etc.
    Static methods are used when the operation you're performing isn't associated with any particular instance of that class--it doesn't use or modify the state of a particular object. The operation is associated with the class as a whole.
    For instance, Integer.parseInt(String). You're not operating on an existing Integer object. Rather you're creating a new Integer from a String. It's a utility or "helper" method of the Integer class.
    And yes, do read the tutorial.

  • Regarding Mediator Component Instance Details

    Hi, i have used file adapter for read and write operation with mediator for the transformation purpose.I get my payload at the output but when i see the mediator instance details at em console window i couldn't see the payload.I only get two tabs one is Audit Trail and other one is Faults. Under Audit Trail i get the option "Expand the : onMessage" but payload is not displayed.Is there any configuration or setting that i need to do so that i could verify my payload at the mediator instance details.
    Regards,
    Saif

    Check the Audit level setting in SOA Infra -> SOA Administration -> Mediator properties (in EM console). If the Audit Level is set to Production, the payload details will not be available in audit trail. Change it to 'Development'.

Maybe you are looking for

  • How do I change the author's name for comments in Pages?

    I am a long time Microsoft Word user and am currently testing out Pages for the first time. Something that is extremely vital to me in my line of work is the ability to track changes and add comments to the documents. I have found how to do this in P

  • Multiple sources in a single Connection Pool

    Hi Folks, I have a requirement to import data from two diffrent data sources( for example : Two diffrent database of Oracle in two diffrent servers) using a single connection pool to the physical layers of OBIEE RPD. Can anyone please suggest whether

  • Stolen iPhone. How get I the info I need to retrieve it?

    I'm in a third world country, and my iPhone's been stolen. I had an opportunity to get my iPhone back today because I saw it on a map briefly but couldn't get it because I couldn't actually pinpoint the location. It was connected to the internet via

  • OS 10.4.6

    Well, since I have a dial up which blazes on at 28.8, I went to the Apple store in Vegas to do my 10.4 to 10.4.6 update. I forgot and had the Call of Duty DVD in the drive but nothing else connected. They let me use their ethernet connection and I do

  • Hi, Please tell me where to get plug in for cs5

    Hello, the Adobe video shows going to view-and down to windows. In perspective drawing Terry White from Adobe opens that window and drags windows to his drawing. My CS5 does not have that and I called adobe and India told me it was a plug in and he c