Reputation: 10948
Contiuned from : This is my question
So now i can use this code :
Window a = getWindow();
a.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Instead of this code :
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
And some of my Senior's said that because of getWindow();
method is returning a Window Object we can use both of the code above.
My question is : why we need to get a returned Window object from this code : Window a = getWindow();
I think when i do this i already have a Window Object
Window a;
But why is it not working?
And my 2nd question is why can't i do like this :
Window a = new Window();
I think it's create a Window object too.
Why must I use the getWindow();
method?
Thanks all
PS: English is not my native language, so sorry if I made some mistakes
Upvotes: 0
Views: 128
Reputation: 1410
Think of it like this. You're doing Object oriented programming, right? You can have multiple instances of an object but they're not all the same thing.
When you use getWindow(), you're getting a specific window. When you just do
Window a; //This will return null since the reference for some window object is created but currently points to null
or
Window a = new Window(ctx); //A window object reference is created which points to some new Window object in memory.
You're referencing a window but not the window you're intending on using.
I hope this clears things up. Please as if you have further questions.
Upvotes: 1
Reputation: 178521
I'm assuming java here:
Window a;
does not create a new Window
object, it only creates a variable, which can hold an object of type Window
. To create the object itself - you need to invoke its constructor, in your case - I assume that what getWindow()
does.
Also, new Window
is a syntax error - constructors in java are invoked similar to methods [with ()
and relevant arguments, if there are any].
Upvotes: 4
Reputation: 11958
You can create an object using it's constructor. Constructor is a method.
Window a = new Window;
is syntaxical mistake in Java. And you cant use a
after Window a;
because in java you have to initialize a variable before using it.
Can you write with a pen just imagining about it? you have to buy that pen at first, don't you?
Window a = new Window();
this will work normally if you write it instead of Window a = new Window;
Upvotes: 2