Results 1 to 14 of 14
  1. #1
    gpveena20 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default global declaration in java

    how to create a global variable,


    public class Globalvar{

    public static MyCustomer objarrcustomer[];

    }

    is this the way to create global variable

  2. #2
    gpveena20 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    public class Globalvar{

    public static MyCustomer objarrcustomer[];

    }
    this is the way to declare a global variable?

  3. #3
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    depends all on what you intend for global

    1) public static Object obj

    this variable is visible from outside this class and is static --> it is a variable of the class, not of the instance of that class (it's the same for every instance)

    you can access it by ClassName.obj

    2) public Object obj

    this variable is visible from outside this class and is not static --> it is a variable of the instance of the class (it's different for every instance)

    you can access it by:
    ClassName instance = new ClassName();
    instance.obj


    3) the best options is to make your variables private (not visible outside of the class) and use Getters and Setters to access it

    Java Code:
    public class ClassName {
    
       private Object obj;
    
       public Object getObject(){
            return obj;
       }
    
       public void setObject(Object obj){
            this.obj = obj;
       }
    
    }

  4. #4
    gpveena20 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    can u tell me how to declare a global variable?

  5. #5
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    There is nothing global in java. In java you have 4 type of access specifiers

    1. default or package level - Accessible in any class within the same package
    2. private - Accessible within the class where it has been declared, no where else
    3. protected - Accessible in any class within the same package and in any subclass in some other package
    4. public - Accessible in any class in the same or different package.

  6. #6
    gpveena20 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    thank u ,

    but i am doing one program,

    in that ther are 3 class library,global var,libraian i stored in different files and accessing by package and import..

    in globalvar i am declaring the globalvariable

    so declare a global variable array,

    public static Mycustomer objArrcustomer[];

    Mycustomer is one of my class, and i am trying to store the customername in objArrcustomer[]

    ao i want to know wat i declare is right or wrong

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Once again, for slow learners, there is no such thing as a global variable in Java.

    Now, you have a public static variable and it can be used, but you need to define it and set it's length before you can start setting element values.

  8. #8
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Yes, it looks ok.

  9. #9
    wolfcro is offline Member
    Join Date
    Feb 2009
    Location
    Italy
    Posts
    51
    Rep Power
    0

    Default

    I see you didn't read or understand what we said to you

    you can declare and access normally a static array like you do now....
    and you can access it from everywere with
    Globalvar.objArrcustomer
    it's not the best way but you can do it

    what we try to do is to teach you how can variables be declared and accessed in Java, and not only "if what you declared is "global" or not"

    in java there's no GLOBAL thing....
    i thought i was understandable when i explained earlier

  10. #10
    gpveena20 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    i got it, thank u to all

  11. #11
    gpveena20 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    can u give me a program for ...
    in a libraray, "the libraian has to register the customer"
    using array simple program

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by gpveena20 View Post
    can u give me a program for ...
    in a libraray, "the libraian has to register the customer"
    using array simple program
    We are not a homework serice.

    Try Rent A Coder: How Software Gets Done -- Home of the worlds' largest number of completed software projects

  13. #13
    gpveena20 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    i stored a name in an array ,if i want to erase a name and i want to use that memory for storing ....how should i do it

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Set that element to null? And to "use it again", simply assign a new value to it.

Similar Threads

  1. How to undefine a Global Variable in JAVA?
    By ajaykushwaha in forum New To Java
    Replies: 13
    Last Post: 11-19-2008, 11:53 PM
  2. Declaration
    By asifahmed in forum New To Java
    Replies: 1
    Last Post: 04-05-2008, 06:38 AM
  3. Global constants
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 10:06 AM
  4. Declaring global variables
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-23-2007, 01:11 AM
  5. JSP Declaration Directive
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-10-2007, 06:42 PM

Posting Permissions

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