• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Static  RSS feed

 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since we cannot create use non-static into static but what when we instantiate the class in static method , the class is not static then why our code is still working ??
 
Pete Letkeman
Ranch Foreman
Posts: 671
18
Android Chrome IntelliJ IDE Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Saad Zahoor,

Do you think that you could provide a few lines of sample code to help clarify what you are talking about?

I think that you may be talking about code kind of like this:

While you can have static classes, which are nested, the outer class cannot be static.
Static methods/members/properties are tied to the class definition and they cannot use any instance methods/members/properties.
Due to this you can call a static method without creating a new object reference e.g. System.out.println ("Hello World").
As a result the static method cannot be assured that there are any instance methods/members/properties available when used/called.
As far as I know static members are generally available from the moment the program starts running, and one of the more used widely static methods is System.out.println().
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so is it wise while coding in android or when developing android app ... When ever i declare variable .. should i declare it static ? or same with the method ..
 
Saad Zahoor
Ranch Hand
Posts: 82
Android
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O i understand. We cannot just invoke the non static method into static method directly .. First we have to create the object then we can declare it ..

where as there is no need to create the object while calling the static method into static method

well thanks for the help
 
Pete Letkeman
Ranch Foreman
Posts: 671
18
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think that you should declare everything as static. From what I understand this would use up extra memory and when dealing with embedded systems like Android there is a very finite amount of memory you would run out of resources in short order.
Static members do not enjoy the benefits of garbage collection where instance members do. However one cannot guarantee that garbage collection even happens. There are definite reasons for declaring members as static and reasons for not doing that as well, which are not just tied to Java/Android.

One of the reasons to declare something as static, is if you have a use for it outside of creating an instance of the object. The main example that comes to mind is when counting resources as listed below:

Other common uses of static methods/members is for helper methods and may not belong to other objects. For instance you may have a class which contains custom static methods that you use for writing to a file or calculating the area of a polygon.
If you add in the final keyword then you can have a class of constants. Maybe you have a database connection string that you use many times or you have a specific directory that you want all of the files to read/write from.
Static members only get initialized once and that is when the program starts and then they live in memory until the program end (generally speaking, there may be some exceptions somewhere).
Where as instance members get initialized when a new instance is created and could be destroyed and memory/resources reclaimed when set to null (depending on garbage collection).

It does appear as though static members have a lot in common with the Singleton programming pattern.

BTW Like you, I am interested in Android app development. As such you may want to be aware that not everything in Java 8 is compatible with Android and something things rely on minimum versions of the Android SDK. I am not saying do not learn Android or do not learn Java, I just want to be aware that there you may run into some issues if you expect to take Java 8 code and have it run without any changes on Android. Google/Android does a document regarding what Java 8 is supported and what is not.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!