Results 1 to 3 of 3
- 04-24-2011, 10:48 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Can any one help me to understand the Code
Hi, I am new to java, please help me to understand below code.
What is the difference?
And which code is better in both application level as well as memory management.
CODE#1
abstract class MyApplicationColor
{
static class Color
{
public static final String RED="red";
public static final String BLUE="blue";
public static final String GREEN="green";
}
}
CODE#2
class MyAllicationColorClass
{
public static final String RED="red";
public static final String BLUE="blue";
public static final String GREEN="green";
}
thanks in advance.
- 04-24-2011, 11:59 PM #2
in the first code the class MyApplicationColor is abstract which means it cant be instantiated or be created as a object(it can be extended or subclassed but you will learn that later). This class would be good to hold data which you can access from anywhere, that goes for methods too. This class is like the Math one in java. So if you where to just access data or use static methods and you dont require to create and object from this class, i would use this one.
As for the second code the class is like the first one but can be created as an object and holds some class variables. if you need to create an instance of this class i would choose that one. otherwise i would choose the first code.
abstract: cant be instantiated but can be used as a superclass.
static: basically means that, the variable or method with it is a class variable or class method and can be called without creating an object of that class. but that variable is the same for all the objects since its a class variable. heres more info on it Java: Static/Class methods
- 04-28-2011, 09:23 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Understand Code
By Quizzle23 in forum New To JavaReplies: 9Last Post: 03-07-2011, 10:07 PM -
need to understand code
By Masken2 in forum New To JavaReplies: 2Last Post: 02-17-2011, 04:21 PM -
understand the code
By prof.deedee in forum New To JavaReplies: 8Last Post: 11-11-2009, 02:43 AM -
help to understand the ? mark in this code
By carolain79@hotmail.com in forum New To JavaReplies: 2Last Post: 10-13-2009, 06:57 AM -
Trying to understand this code
By new2java2009 in forum New To JavaReplies: 2Last Post: 09-09-2009, 07:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks