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);
                        break; 
                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(); 
        application.add(panel);
        application.setSize(250,250);
        application.setVisible(true);


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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.