In this code i did not understand something. In one class, a variable is declared as private, but then this value is used in another class.

import java.awt.Graphics;
import javax.swing.JPanel;

public class DrawRect extends JPanel {
    private int choice; //user's choice of which shape to draw.

    public DrawRect (int userChoice){
        choice = userChoice; 
    }//End constructor

    public void paintComponent(Graphics g){
        super.paintComponent( g );
        for (int i = 0; i < 10; i++){
            switch (choice)
                case 1; 
                    g.drawRect(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10);
                case 2; 
                    g.drawOval(10 + i * 10, 10 + i * 10, 50 + i * 10, 50 + i * 10);

            }//end switch
        }//end for
    }//end method
}//end class

now, driver class:

//berk elmas
import javax.swing.*;

public class Arrays {
    public static void main(String[] args){

        String input = 
                JOptionPane.showInputDialog("Enter 1 to draw rectangles"+
                                            "Enter 2 to draw Ovals");

        int choice = Integer .parseInt( input );//convert input to integer 

        DrawRect panel = new DrawRect(choice); // here, how can choice instance variable be used ? 
                                               // isn't it declared in another class as private? 

        JFrame application = new JFrame(); 

    }//end method main. 
    }//end class. 

I know the name of the driver class has nothing to do with the fast of the class.

6 Years
Discussion Span
Last Post by GeekTool

sorry guys i have just understood. It is midnight in my place. Thats why i am so confused :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.