Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #20080

Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream()

Newsgroups comp.lang.java.programmer
Date 2012-12-03 14:04 -0800
References <3c227741-7b7f-4cf8-b188-ce5268894031@googlegroups.com> <k9j0qj$ftg$1@dont-email.me>
Message-ID <890512f9-9df6-42dd-a151-993e09912ece@googlegroups.com> (permalink)
Subject Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream()
From Lew <lewbloch@gmail.com>

Show all headers | View raw


markspace wrote:
> zyng wrote:
>> I am sure the difference between the two have been discussed well:

Between the two ...?

Oh, you think the subject line is part of the post. Oops.

>> the first one is using the System classloader and the second one is

The "first one" being 'ClassLoader.getSystemResourceAsStream()'?

The "second one" being 'Class#getResourceAsStream()'?

>> using the class loader which has loaded this class, most likely a
>> child of System classloader, according to what I have found on the
>> web.
> 
> First, note that there are two separate objects here, ClassLoader and 
> Class.  Just because the names of their respective methods are the named 
> the same, doesn't mean they do the same thing.

The names of the respective methods in this case are *not* named the same!
 
'ClassLoader' also has a method 'getResourceAsStream()', which unlike 
'getSystemResourceAsStream()' is not static and does different things.

>> //option 1: InputStream is =
>> ClassLoader.getSystemResourceAsStream("hij/hello.txt");
> 
>> //option 2: InputStream is =
>> ResourceTools.class.getResourceAsStream("/hij/hello.txt");
> 
> Note in the documentation of the second method call:
> " This method delegates to this object's class loader. If this object 
> was loaded by the bootstrap class loader, the method delegates to 
> ClassLoader.getSystemResourceAsStream(java.lang.String)."
> 
> So it's different because it delegates to a different method.  I have to 

And has a different name and is documented to do different things.

> guess that the two methods are designed to do different things, in spite 
> of their identical [sic] names.  I suppose this could be considered a 
> "gotcha."  My advice is to consider option 2; I've never seen the first 
> form in actual practice.
> 
> Why they did things this way, I have no idea.  Classloading pretty 
> confusing except in trivial cases.  I think it's best to keep things as 
> simple as possible when you resort to loading manually.

The OP also said, 
> The first one cannot start with "/" and the second one must start with "/". 

Not true.

> I found this is very confusing since the code between the two are already very similar looking. 

Even more confusing since it isn't true.

And the two aren't all that similar, in that they have different names and are in different classes.

If you think that's confusing, wait until you discover 'ArrayType', 'Attribute', 'BasicAttribute' vs. 
'BasicAttributes' (even more similar than the two that confused you), 'Binding', 'Bindings', 'Boolean' vs. 
'boolean' (also more similar than what confused you), 'Bounds', 'Certificate', ...

What about the fact that the 'close()' method of 'Closeable' implementations differ from each other 
yet are all part of the same type? Ore isn't the same as 'ResultSet#close()' prior to Java 7? And is 
not at all the 'close()' of 'StreamHandler'?

'List#get()' vs. 'Map#get()' - very different.

'Date#getTime()' vs. 'Calendar#getTime()'

This is fun.

-- 
Lew

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() zyng <xsli2@yahoo.com> - 2012-12-03 11:29 -0800
  Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() markspace <-@.> - 2012-12-03 12:07 -0800
    Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() Lew <lewbloch@gmail.com> - 2012-12-03 14:04 -0800
      Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() markspace <-@.> - 2012-12-03 15:25 -0800
        Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() Lew <lewbloch@gmail.com> - 2012-12-03 20:37 -0800
  Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() zyng <xsli2@yahoo.com> - 2012-12-04 05:40 -0800
    Re: ClassLoader.getSystemResourceAsStream versus this.getClass().getResourceAsStream() Lew <lewbloch@gmail.com> - 2012-12-04 12:57 -0800

csiph-web