Results 1 to 4 of 4
  1. #1
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Question Static and non staic reference, Synchronized and unsynchronized, abstarct class,

    Hi to all :),
    Hope you all will be fine.Actually i want to know the answers of some questions to clear my confusion

    1. Why we not reference a static method from a non static context. What the reason?
    2. We say static mean belonging to whole class what is mean by belonging to whole class because it's also possible to make an instance of class and then access static method using object instance?
    3. What is mean by when we say that HashMap is unsynchronized and Hashtable is synchronized?
    4. Similarly Vector is synchronized whereas arraylist is not?
    5. Why we not instantiated abstract class, What's the reason? It is possible to make class abstract without declare any method abstract, so why we declare abstract methods?when we make our class abstarct?


    if I have a code like this how can i make a constructor and use super

    Java Code:
        public class Car {
    
            private double regularPrice;
     
            private Car(double regularPrice){
                this.regularPrice = regularPrice;
            }
        }
    
        public class Ford extends Car{
    
            private Ford(double regularPrice,  int year, int manufacturerdiscount){
                 super(regularPrice);
                 this.year = year;
            }
        }
    Means when i use private constructor how can i make instance of it and how i use super keyword to access super class members? Also when we use private constructors?, is it a good programming practice to make constructor private?

    Please give me answer of these questions.
    Thanks in advance
    Last edited by Basit56; 08-16-2009 at 01:41 PM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    "Why we not reference a static method from a non static context. What the reason?"
    I think you have it backwards? Perhaps I am misunderstanding. You can reference static things from a non-static context, but you cannot have a static thing reference something that is not static. For example:
    Java Code:
        static int num = 4;
        
        public void test(){
            int myNum = num;
        }
    works fine but this does not:
    Java Code:
        int num = 4;
        
        public static void test(){
            int myNum = num;
        }
    - "We say static mean belonging to whole class..."
    Static actually means that the variable/method does not require instantiation. So, what you said is basically correct, but understand why. Static variables and methods exist wether or not an object of that class type is instantiated. For instance:
    Java Code:
    Color myColor = Color.RED;
    RED is a static Color object in the color class. You can make use of it without instantiating a color object.
    -"What is mean by when we say that HashMap is unsynchronized and Hashtable is synchronized? ..."
    Synchronized means it is thread safe. Multiple threads can use the object without fear of concurrent thread access. So when something is unsynchronized, there is no guarantee that two threads cannot muck with data at the same time.
    -"Why we not instantiated abstract class, What's the reason? It is possible to make class abstract without declare any method abstract, so why we declare abstract methods?when we make our class abstarct?"
    The definition of an abstract class is to define what an object will do but not how it does it. This preserves Object Oriented Relationships and loose coupling in the sense that it forces other programmers making use of an abstract object to define certain methods, but gives them the freedom to tailor the working of those methods to their particular application.

    A private constructor can be used if you specifically do not want someone to instantiate an instance of your object. Consider using public, protected, or package in this case instead of private.

    Hope that helps!

  3. #3
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default

    Hi :),
    Thanks for your reply.That's great but i want to clear more.The code
    Java Code:
        static int num = 4;
        
        public void test(){
            int myNum = num;
        }
    is an example of referencing static variable from non static context, is it? and reverse is not true, and from the code
    Java Code:
        int num = 4;
        
        public static void test(){
            int myNum = num;
        }
    Since we can not access instance variable num in the test method but when num was static we can access it in the test method that's why we say that static belong to whole class

    Also in the code regarding private constructor i try the following

    Java Code:
             import java.lang.reflect.Constructor;
    
    
             public class MyOwnAutoShop {
    
                public static void main(String[] args) throws InstantiationException{
    
                   try{
                         Constructor[] cons = Car.class.getDeclaredConstructors();
                         cons[0].setAccessible(true);
                         Car car1 = (Car)cons[0].newInstance(null);
                   }catch(Exception e){
                         e.printStackTrace();
                     }
             }
    First i try this without try catch so it said, throw an exception must be caught so i put it in the try catch block but at line

    Car car1 = (Car)cons[0].newInstance(null);

    it shows an yellow exclamation mark, i m using netbeans6.5, which contain messages

    non-varargs call of varargs method with inexact argument type for last parameter;
    cast to java.lang.object for a varargs call
    cast to java.lang.object[] for a non-varargs call and to suppress this warning

    So what this mean, i try to change (Car) with (Object) but it show message incompatible types, then i replace both Car and (Car) with Object, but it again shows above messages. So what's the problem now.


    Thank you very much :)

  4. #4
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default

    Hi :),
    hope you will be fine.After removing null the warning disappear , do you tell me what role this null playing here and if i use any integers and float there is no warning also

    Car car1 = (Car)cons[0].newInstance(null); //display warnings

    Car car1 = (Car)cons[0].newInstance(); // no warning

    Also i instantiate private constructor, the same i did for Ford class

    Java Code:
        try{
                 Constructor[] cons1 = Ford.class.getDeclaredConstructors();
                 cons1[0].setAccessible(true);
                 Car ford1 = (Ford)cons1[0].newInstance(500000, 2004, 20);
            }catch(Exception e){
                e.printStackTrace();
             }
    but in the Ford class when i call super(regularPrice) it again says Car has private access so how can i use super() after instantiate private constructor.

    Thanks in advance

Similar Threads

  1. why cant a top most class has staic modifier
    By srikantha in forum New To Java
    Replies: 4
    Last Post: 08-06-2009, 06:03 PM
  2. Replies: 3
    Last Post: 04-24-2009, 06:28 PM
  3. Passing Class Reference to method
    By nekt in forum Advanced Java
    Replies: 5
    Last Post: 03-26-2009, 05:08 AM
  4. Please help, need to make my class static.
    By sumak in forum New To Java
    Replies: 1
    Last Post: 04-19-2008, 07:29 AM
  5. Static class
    By eva in forum New To Java
    Replies: 1
    Last Post: 12-31-2007, 01:02 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
  •