Results 1 to 3 of 3
  1. #1
    soomroimran is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    Kyle227's Avatar
    Kyle227 is offline Member
    Join Date
    Apr 2010
    Location
    Mass
    Posts
    47
    Rep Power
    0

    Default

    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

  3. #3
    soomroimran is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks dear,
    your post really help me to understand where I use my CODE#1 and where should I use CODE#2.

Similar Threads

  1. Understand Code
    By Quizzle23 in forum New To Java
    Replies: 9
    Last Post: 03-07-2011, 10:07 PM
  2. need to understand code
    By Masken2 in forum New To Java
    Replies: 2
    Last Post: 02-17-2011, 04:21 PM
  3. understand the code
    By prof.deedee in forum New To Java
    Replies: 8
    Last Post: 11-11-2009, 02:43 AM
  4. help to understand the ? mark in this code
    By carolain79@hotmail.com in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 06:57 AM
  5. Trying to understand this code
    By new2java2009 in forum New To Java
    Replies: 2
    Last Post: 09-09-2009, 07:18 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •