Why are static methods called with null references,valid ?

This is my code :
package inheritance;
public class inh6{
     public static void method(){
     System.out.println("Called");
     public static void main(String[] args){
          inh6 t4 = null;
     t4.method();
O/P :
CalledHere t4 is a null reference and yet we are able to call a method on it,why is null pointerexception not thrown.Why are we able to call static methods using null references ?
t4 is null means it doesnot refer to any memeory address,hence how is method() called correctly.
I hope i am clear. :)
Thank you for your consideration.

punter wrote:
jverd wrote:
Memory addresses have nothing to do with it. I doubt memory addresses are even mentioned once in the JLS.
By memory address i mean the memory location the reference is pointing to.I know what you mean. But if you think it's relevant, can you show me where in the JLS it says anything about memory locations?
>
You can do that because a) t4's type is "reference to inh6" and b) method() is declared static, which means that you don't need an object to call it, just the class. That class comes from the compile time type of t4. The fact that t4 is null at runtime is irrelevant.
So at compile time the type of t4 is inh6 and hence the method is called.Is it ? Had method() not been static a NullPointerException would have been thrown.Correct.
With non-static, non-private, non-final methods, which implementation of the method gets called is determined at runtime, buy the class of the object on which it's being called. If any one of those "non"s goes away, then the method is entirely determined at compile time, and in the case of static methods, there's no instance necessary to call the method in the first place.

Similar Messages

  • Static method called from a null object reference

    Hello folks,
    Here´s the code:
    public class TestClass {
         public static void main(String[] args) {
              StaticNull _null_ = null;
              _null_.testNull();
    class StaticNull {
         public static void testNull() {
              System.out.println("testNull");
    }I know that a static method belongs to the class, so you don´t need an
    instantiated object of that class to call it. But in the code above, as
    I'm calling the testNull method from a null reference, shouldn't a NullPointerException
    be thrown?
    Best regards,
    Danniel

    sometimes wrote:
    yawmark wrote:
    Calling static methods from a reference variable should be considered a bad practice. Our coding standards prohibit it, and I suspect we're not the only ones.
    ~what are you trying to say? your coding standard encourages what tricks to invoke static methods? i mean other than using a 'reference variable'?I think you are misreading yawmark's comment. He was saying that invoking a static method using a reference variable -- as though the method weren't static:
    var.staticMethod();...is bad practice. His shop's coding standards prohibit it. And that's a common coding style standard. IDE's often warn you of that, right?
    edit: and judging by your reply #7, you and yawmark would bond over a few jars of cold beverages. You seem think with one mind.

  • Why are my current calls being interrupted by new calls?

    Why are my current calls being interrupted by new calls?
    I'm using iPhone 6 plus and reset my network settings multiple times. The issue is random.

    glic1 wrote:
    Why are my current calls being interrupted by new calls?
    I'm using iPhone 6 plus and reset my network settings multiple times. The issue is random.
    Are you saying when your on a phone call and someone else calls you it does what exactly?  Disconnect your current call with the new call?

  • ADF Mobile: WebService data control method call with array

    JDev 11.1.2.3
    ADF Mobile deployed to Android emulator
    Hello All,
    I am trying to invoke a method in my Web Service data control and get the following exception
    Caused by: ERROR [oracle.adfmf.framework.exception.AdfInvocationRuntimeException] - Cannot serialize: [I@1dbae822
            at oracle.adfmf.dc.ws.soap.SoapTransportLayer.invokeSoapRequest(Lorg/ksoap2/SoapEnvelope;)Ljava/lang/Object;(Unknown Source)
            at oracle.adfmf.dc.ws.soap.SoapWebServiceOperation.invoke(Ljava/lang/String;Loracle/adfmf/dc/ws/soap/SoapGenericType;)Ljava/lang/Object;(Unknown Source)
            at oracle.adfmf.dc.ws.soap.SoapWebServiceOperation.invoke(Ljava/lang/String;Ljava/util/Map;)Ljava/lang/Object;(Unknown Source)
            at oracle.adfmf.dc.JavaBeanOperation.execute(Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;(Unknown Source)
            at oracle.adfmf.dc.ws.WebServiceDataControlAdapter.invokeOperation(Ljava/util/Map;Loracle/adfmf/bindings/OperationBinding;)Z(Unknown Source)
            at oracle.adfmf.bindings.dbf.AmxMethodActionBinding.execute()Ljava/lang/Object;(Unknown Source) This method is an AppModule method exposed as a service interface and the parameter for this method is a List<oracle.jbo.domain.Number>. The schema definition for the input is as follows:
    <element name="acceptTask">
       <complexType>
         <sequence>
                <element maxOccurs="unbounded" minOccurs="0" name="taskID" type="decimal"/>
         </sequence>
       </complexType>
    </element>For the input to my binding, I have tried int[], Integer[] and List<Integer>. All of these result in similar errors.
    I have also tried invoking this through a regular ADF application and that works fine with an int[]. It looks like something specific to the ADF Mobile SOAP layer.
    Is this a bug or a restriction in the framework? Any workarounds that has worked for anyone?

    No luck. A WS DC method call with a simple parameter (java.lang.String or java.lang.Integer) works fine but I can't get it to work when there is an array input.
    I have tried WS methods with int arrays and simple string arrays without any luck. All of them result in a cannot serialize error.
    I can't figure out what I am doing wrong. Are there any working WS Datacontrol samples with array inputs?

  • Initializer block not called when static method called

    public class Initializer {
         Initializer(){
              System.out.println("Constructor called");
                   System.out.println("CLASS INITIALIZED");
         static void method(){
              System.out.println("Static method called");
         public static void main(String[] args) {
              Initializer.method();
    From the JLS
    A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
    T is a class and an instance of T is created.
    T is a class and a static method declared by T is invoked.
    [b]
    But when i call the static method , if the class is initialized shouldnt the initializer block be called, it is not called, why.

    Perhaps running something like this will add a little more colour?:
    public class Initializer {
        static {
            System.out.println("First static initializer");
            System.out.println("First instance initializer");
        Initializer() {
            System.out.println("Constructor");
        static {
            System.out.println("Second static initializer");
            System.out.println("Second instance initializer");
        static void staticMethod() {
            System.out.println("staticMethod");
        void instanceMethod() {
            System.out.println("instanceMethod");
        public static void main(String[] args) {
            System.out.println("main");
            staticMethod();
            new Initializer().instanceMethod();
    }

  • Why are both SQL_ID and PREV_SQL_ID null?

    session A is blocking session B.
    it was row-level lock.
    But, why are both SQL_ID and PREV_SQL_ID null in session A?

    Well, what do we know?
    Session A must have an uncommitted transaction.
    That uncommitted transaction is blocking session B.
    But session A could well be doing nothing now, could be churning away doing some sort of processing outside of the database (java client you said), might well be off doing something elsewhere in a distributed transaction.
    Who knows?
    Not necessarily the database.
    If you have ASH licensed, you might, might get a better picture of what session A was doing by looking at DBA_HIST_ACTIVE_SESS_HISTORY but as this is sampled data and your statements might fly in and out, it might not be helpful.
    Note that even if prev_sql_id or sql_id were populated, these might not be the statements responsible for the lock.

  • What are the methods called while navigating from one applet to another one

    Hi All,
    Could any one brief me about "When you navigate from one applet to another what are the methods called ?".
    Thanks in advance.
    Best Regards,
    N.Madhusudhanan.

    http://forum.java.sun.com/thread.jsp?forum=421&thread=426771&tstart=0&trange=100

  • Are static methods in Java thread safe?

    Are static methods in Java thread safe?
    thanks,
    suresh

    if static method use the instance variable
    You mean member variable, where member variables are either class variables (static) or instance variables (non-static).
    then you have to make it thread safe using
    synchronization Not necessarily. Depends on requirements and usage context.
    else in case of local var. it
    is thread safe. Not necessarily. That local variable could refer to an object that's visible to multiple threads.
    Statements like "Local variables are threadsafe but member variables aren't" are an oversimplification.
    Something is threadsafe if it cannot be made to behave incorrectly simply by running it in a multithreaded context. Determining that can be very difficult.

  • My internet security picked up on this. Why are you updating me with a trojan program ????????? ffwt.html Blocked: HEUR:Trojan.Script.Generic 4/18/2013

    My internet security picked up on this.
    Why are you updating me with a trojan program ?????????
    <pre>ffwt.html Blocked: HEUR:Trojan.Script.Generic 4/18/2013 8:13:18 PM hxxp://incasi<i></i>.xtrastudio<i></i>.com/
    downloading object, which contains a trojan program
    firefox
    hxxp://incasi<i></i>.xtrastudio<i></i>.com/ffwt<i></i>.html
    detected: HEUR: Trojan.Script.Generic</pre>
    (Moderator altered links, because they are malicious)

    Could you explain what you mean by "updating"? Did your antivirus pick this up while Firefox was updating or while you were browsing the Internet?
    If you were browsing the Internet (While visitting the hxxp://xtrastudio<i></i>.com link), then that website was infected and not a Firefox problem. I would advise you to not visit the website again though, unless they manage to fix the infection as the antivirus managed to block it now, but it might not be able to the next time.
    If it happened during the "Firefox is updating" window when you tried to start Firefox, then it pretty serious.
    If you could answer that question, we will probably be able to help you. Good luck :)

  • Are DLL methods called as instance or static methods from Java ?

    Hi,
    I have only a small question.
    When I call a C++ DLL method from Java using JNI, do I instantiate it like an object method or is it called as a static method ?
    I am asking you this because I will use servlets (Java 1.2.2) to call a DLL. These servlets may run concurrently in any moment, and I pass some data to this DLL. I want to know if there is any possibility of this data becoming corrupt, because of one concurrent servlet rewriting the data another servlet has passed.
    Thanks and regards.

    The question is not specific enough.
    Methods in C/C++ can be qualified with the following forms:
    -global (C/C++)
    -static member(C++ class method)
    -instance member(C++ class method)
    Since you didn't provide your code I couldn't possible determine which of the above you are using, but presumably you know.
    On the other hand a java native method is either static or non-static depending on how you defined it in the class. Once again without the code there is no way to say and presumably you already know this.
    The context of the methods is the same context that a similar servlet method has. But naturally in C/C++ is is easy to circumvent the context - for example by using a global variable.
    Data in C/C++ can always become corrupt and in the vast (all?) cases it is because something is wrong in the C/C++ code. Like a pointer that is not initialized, or a pointer used after it is free'd. Or writing past the end of a buffer, or writing before the beginning of a buffer.

  • Taskflow Method call receiving null parameter.

    Hi all,
    I am using 11.1.1.6. I have created in my application an extra project which is pure Java objects and exposed a master class as a POJO DC. In my application, I have a taskflow where I have dragged and dropped one method from my POJO DC - 2 of the parameters of this methods are coming from an application scope bean. I have debugged the application, and made sure that the object being returned by the getter of my app scope bean is not null. So basically, when the breakpoint is in the return statement of my getter the oject is not null and it has been correctly initialized. Just after that, the next breakpoint is in the class receiving the parameter in my POJO DC class. In there, the object is NULL.
    Does anyone knows wat could be the reason??

    Hi Frank Nimphius-Oracle,
    That is precisely the problem.  The object is being passed as to the taskflow as an input parameter (getting it from my application scope bean). If I access the pageFlowScope inside my taskflow I see it and its there, correctely intialized. However, when I call a method call activity that consumes that object as parameter, all what it gets is null.
    The method that consumes this object is in a separate project, and its exposed in a POJO DC. I don't know if it has to be with the complexity of the object I am passing or what but I don't understand why its not being passed correctly to the DC Method.

  • Databind method call returns null in WebCenter Content Filter

    Hi,
    I have inherited some code that has an issue and I'm new to WebCenter Content and the Filters that can be added. We have a FileNameFilter class that starts off with the code
    public class CWEFileNameFilter implements FilterImplementor {
        public CWEFileNameFilter() {
            super();
        public int doFilter(Workspace workspace, DataBinder dataBinder,
                            ExecutionContext executionContext) throws DataException {
            String service = dataBinder.getLocal("IdcService");
    However, service sometimes comes back null and then the filter throws a NPE.
    My questions are:
    1) Can anyone explain what dataBinder.getLocal("IdcService"); is doing and why it could come back as null
    2) Can anyone provide any links to documentation that explains Filters and a bit more about the DataBinder object as the JavaDoc isn't much help.
    Thanks
    Marc

    Hi Marc,
    Below code will give service name for which filter is implemented  from LocalData
    String service = dataBinder.getLocal("IdcService");
    for example below
    http://localhost:16200/cs/idcplg?IdcService=DOC_INFO_BY_NAME&dDocName=1111111&IsJava
    This will print entire response data including localdata and results which are created as part of service execution. Below is snippet
    <?hda version="11.1.1.8.0PSU-2015-04-01 00:14:53Z-r126309" jcharset="UTF8" encoding="utf-8"?>
    @Properties LocalData
    DocUrl=https://localhost:16200/cs/weblayout/groups/secure/documents/test/mhdk/mjg1/1111111.pdf
    IdcService=DOC_INFO_BY_NAME
    so String service = dataBinder.getLocal("IdcService"); will be service ="DOC_INFO_BY_NAME".
    Below blog will give some idea about filter but best place to learn or start is "The Definitive Guide to Stellent Content Server Development" by Brian Huff
    http://www.redstonecontentsolutions.com/technical-blog/ucm-service-handlers-and-javafilters
    Also i would recommend reading "WebCenter Content Services Reference Guide"
    http://docs.oracle.com/cd/E23943_01/doc.1111/e11011/toc.htm
    Regards,
    Amol Gavali.

  • How are static methods handled in a multithreaded application?

    hi
    i have a class Drawer with a static method public static draw(Graphics g). now assume there are more thrads callin at the same time Drawer.draw(g). What happens? have some threads to wait until the others have finished calling the method, or can static methods be multithreaded, (can they perform simultaniously)?
    thanks, jo

    ups, i am not drawing on the screen, but in every thread i am drawing a Image. this means before i call the static method in every thread there will be created a BufferedImage and then i pass the Graphics from this images to the static method. also in this case the method performs simultaniously? every thread has its own image and graphics, so the static method should be perform at the same time, isn't it?

  • Why have static methods?

    What is the criterion to decide whether a method should be static or not?

    lets say you have a class called Utility. No other class ever needs an instance of Utility becuase all of it's methods are static and the class is public. Example:
    package com.abc.util;
    /* make it final so can't extend */
    public final class Utility
       /* can't call new Utility now */
       private Utility()
       /* these methods below, by just seeing their names, should be static */
       public static long ipStringtoLong(String address){}
       public static String execShellCMD(String shellCMD){}
       // ect ...
    }Now a class can import and use it. There are no instances associated with these methods except the values passed to them. It makes them accessable from anywhere in your application w/o worrying about having a ref to an Object. Their accessable as long as the class can be found in your class path. Jar'em up and put them into C:\jdk1.xx\jre\lib\ext\util.jar
    import com.abc.util.Utility;
    // ........(String host, String port){
       long ipLong = Utility.ipStringToLong(host);

  • Problem with null reference

    Hey,
    I have a "Window" class wich extends JFrame and then creates a tabbedPane. In each of the tabs it's necessary to access to another class (Code).
    Each of the tabs follows something like this:
    public class Tab1 extends JPanel {
       private Code code;
       public Tab1 ( Code c ) {
          code = c;
    }When the tabs are created they receive the Code object.
    The Code object declared in the main class and passed into the "Window" class (still a null reference) and then to each of the tabs (null). The instantiation of the Code object is done in one of the tabs. The Other tabs need it to set some values.
    The problem is the tab that instantiates the Code object is doing so, however the other tabs don't get the new instance (it's always null). Although it's always the same reference.
    Any ideas?
    Thanks.

    It looks like setting the Code is something the Window object will
    have to help with.//in Window (assuming Tab is a superclass of Tab1 Tab2
    Tab[] tabArr = {new Tab1(null, this), new Tab2(null, this)};
    void setCode(Code c) {
        for(Tab tab :tabArr) tab.setCode(c);
    //then the Tabs look like
    public class Tab1 extends Tab { // or extends JPanel implements Tab
        private Code code;
        private Window wind
        public Tab1( Code c, Window w ) {
            code = c;
            wind = w;
        void setCode(Code c) {
            code = c;
        void someEventPerformed() {
            Code c = null;
                // figure out what flavour of Code the user wants
            switch ( userOption ) {
                case 0:
                    code = new Code1 ();
                    break;
                case 1:
                    code = new CodeY ();
                    break;
                // and have the Window tell all the Tabs to use it
            wind.setCode(c);
    }

Maybe you are looking for

  • For the first time I hate Firefox! Important time-saving feature removed (4.0)

    For the first time I hate Firefox (4.0). They removed the feature for the user to jump back multiple pages at once. I use this MANY MANY times a day and now I have to go back one page at a time until I get back to where I want to go. IF I have to go

  • SAP R/3 connection with XI using an ABAP Proxy

    Have to develop an ABAP program which will communicate with XI using an ABAP Proxy.

  • Adding to the end of a file

    hi guys, im tryin to open a file then add changes to the end of it, at the mo im using the following but it doesnt add to the end, any ideas? try { BufferedReader br = new BufferedReader(new FileReader("H:\\new folder\\TransactionFile.txt"));      Bu

  • How to change the color of letters instead of highlight the text when compare two documents

    Hello!! I have the Adobe Acrobat 9 Pro (Versión 9.0.0) and I need to compare two documents, but I want to show the diferences changing  the color of letters instead of highlight the text with any color. My boss tell me that other Acrobat version have

  • Chinese language issue with I18N

    Hello all,      We are implementing i18n in our project. And for that, we have a resource bundle with a chinese properties file with all chinese characters. And i have saved that file with UTF-8 encoding. Then, when i run my application, iam getting