I have read some reference’s and i think i understood the usage the “new” keyword in OOP languange (but i don’t!)
So i can’t understand this line of code, i got it from the tut’s in thenewboston :
ourBrow = (WebView) findViewById(R.id.wvBrowser);
ourBrow.setWebViewClient(new OurViewClient());
So the setWebViewClient(); method requires a WebViewClient in it’s params. The think that i don’t understand is, why we need to add the “new” keyword here?
NOTE that OurViewClient() is a class that we made and extended by WebViewClient.
Sorry, if my way to ask the question is making you confuse, coz i also confuse now @_@
Thanks All! 😀
NOTE : English is not my native languange, so sorry if i made some mistake’s 😀
OurViewClientis the class that you made.OurViewClient()is a constructor for that class which takes 0 parameters. And thesetWebViewClient()method requires aWebViewClientinstance as its parameter.So the
newoperator effectively allocates a new instance of theOurViewClientclass and then invokes the default 0-parameter constructor on that instance, returning the created object. Usingnewis the only way you can create an instance of anObjectin Java, barring more advanced topics like reflection or using things likesun.misc.Unsafeand also some exceptions centered around built-in types/autoboxing (e.g.String s = "str";andInteger num = 7;).Note that the following code is essentially equivalent to the code that you have:
Also note that the following are invalid: