Closed Thread
Results 1 to 4 of 4

Thread: Hey, first post! Very very basic java questions..

  1. #1
    Did
    Did is offline Newbie
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Hey, first post! Very very basic java questions..

    Hey, I'm brand new to the forum; just signed up.

    Anyway, Im taking a Java programming class right now in college, yet the teacher is horrible doesn't explain anything. She currently just gives us code that we copy and paste into text pad, compile and then run.

    So because of this, it leaves everyone full of questions, tis why I need a few answers.
    Also I'd appreciate it for the answers to be dumbed down a bit..

    1. In the current program she has us writing we are importing 2 files
    Code:
    import java.awt.*;
    import java.awt.event.*
    Now from my understandings, isn't * a wildcard and wouldn't it import everything in java.awt in the first line?

    2. Shortly after we have,
    Code:
    public static void main(String[] args) { some code etc }
    Does public mean that the following code is a class? Also are classes like functions as in I can call them later in the program? Also what exactly does static/void mean. I assume main means its the start of the program or main program.

    3. Last question I have is with this piece of code:
    Code:
    public void windowClosing(WindowEvent e)
    	{
    		System.exit(0);
    	}
    Now after WindowEvent, there is an e. Is this a variable? or like a parameter or something?

    Thanks for any replies!

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,881
    Rep Power
    24

    Re: Hey, first post! Very very basic java questions..

    1).. i actually don't know myself :s
    2) 'public' means other classes can see/use it.
    Static = the class where this part is written in doesn't has to be instantiated to let this method being used.
    Void. This method doesn't return something

    example 1.
    Code:
    public class MyClass{
      private int year;
      
      public MyClass(int year){
        this.year = year;
      }
    
      private void doubleYear(){
        year*=2;
      }
    
      public int getYear(){
        return year;
      }
    
      public static String whatIsThis(){
        return "this class has only one attribute: year";
      }
    A class is defined with the "class" wordt, eg public class MyClass or class YourClass or private class PrivateClass.

    The constructor looks like 'public/private/protected <className> (...){ };
    This will create an object of the class. Or some people may say "instantiate the class", thus creating an instance of this class.
    Code:
    private void doubleYear(){
        year*=2;
      }
    This method is "private" So if you had a 2nd class looking like:
    Code:
    public class Second{
      MyClass test = new MyClass();
      test.doubleYear(); //error here
    This would fail, because the class "Second" does not has acces to the doubleYear() method as it's declared private.

    The void means it returns nothing.
    In the next method it does:
    Code:
    public int getYear(){
        return year;
      }
    This one returns an integer + can be accessed from outside as it's public:
    Code:
    public class Second{
      MyClass test = new MyClass();
      int year = test.getYear();

    Static methods may be somewhat more complex:
    Code:
    public static String whatIsThis(){
        return "this class has only one attribute: year";
      }
    So you don't need an object of the class to use it.
    Note that in the previous 2 examples i always created an instance, using "new MyClass();" And then i accessed the methods with "test.xx()"

    Static methods don't need an instance an can be called directly:
    Code:
    public class Third{
      String output = MyClass.whatIsThis();
      System.out.println(output):
    }
    You may have used Integer.parseInt() as static method allready without knowing.


    3) the e is a variable that you can use to get more information of the windowEvent.
    In a different case and you would have a KeyEvent e. You can use the e to look up which key of the keyboard exactly was pressed for example.

  4. #3
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Hey, first post! Very very basic java questions..

    Quote Originally Posted by Did
    Anyway, Im taking a Java programming class right now in college, yet the teacher is horrible doesn't explain anything.
    Tells you how useful college is...

    Quote Originally Posted by Did
    1. In the current program she has us writing we are importing 2 files
    Code:
    import java.awt.*;
    import java.awt.event.*
    Now from my understandings, isn't * a wildcard and wouldn't it import everything in java.awt in the first line?
    * is a wildcard character, but behaves a little differently than I think you yet understand it to. The wildcard will only import all of the classes available in the direct java.awt package, not any sub packages of java.awt, which happens to include event. Packages are divided in a hierarchical system that allows you to place packages in other packages. The import statement very carefully does NOT import other packages within a package when you use the * character, simply because it would do more harm than good. Imagine if someone said "import *"?

    Quote Originally Posted by Did
    2. Shortly after we have,
    Code:
    public static void main(String[] args) { some code etc }
    Does public mean that the following code is a class? Also are classes like functions as in I can call them later in the program? Also what exactly does static/void mean. I assume main means its the start of the program or main program.
    That is not a class, that is a class method. There's quite a bit of abstract goings on involved with this, so I'll try to carefully explain it. If you look above that line of code you should see something along the lines of "public class NameOfClass" (insert the name, obviously), that's the actual class declaration, and everything that is within it is nested in the NameOfClass class. Classes may contain properties, methods, or other nested classes. The properties and methods may also be either static (named "class methods" for methods, or "static fields" for properties) or non-static (named "instance methods" for methods, or "instance variables" for properties), and their differences are important. Here's a class with one of each:
    Code:
    public class Example {
        public static void classMethod() {
            // This method is a class method, which means it may be ran without an
            // instance of the class to run from. Because of this, non-static properties,
            // methods, and the this pointer are inaccessible from these methods.
            // Here's an example of using it:
            // Example.classMethod();
        }
    
        public void instanceMethod() {
            // This method is only accessible from an instance of the class, IE after you make
            // a "new" Example class. For example:
            // Example e = new Example();
            // e.instanceMethod();
        }
    
        // The following is a static field. This means it also does not require an instance
        // of the Example class to be run and can be accessed from static methods.
        // For example:
        // Example.classProperty = 5;
        public static int classProperty;
    
        // The following is an instance variable, it can only be accessed when an instance
        // of the Example class exists. For example:
        // Example e = new Example();
        // e.instanceProperty = 5;
        public int instanceProperty;
    
        private class NestedClass {
            // This is a nested class. It can be treated in every way like a normal class
            // with the marked exception that it may also be protected or private.
            // These are useful when you want a class that is intrinsically linked to the
            // containing class, and thus need access to it's private properties and
            // methods. This replaces "friend" access in C++.
        }
    }
    So, now on to explain what the main class method is, which all Java programs must contain, and you are correct in assuming that "main" means the very first part of the Java program to be run.
    Code:
    public static void main(String[] args) {
        /* 
           I'll go one at a time. "public" means that the method is accessible by objects
           that are not related to this class. It essentially means any part of the code
           has free access to this method and may run it at any time. You need to give
           main public access.
    
           "Static" means that this is a class method, and as such may be accessed
           without having an instance of the class in existence. The main method must
           be static, since there won't be an instance of the containing class when the
           program is first run.
    
           "Void" means that the method does not return any value. Most methods will
           return a value, but some do not and simply perform some sort modification to
           the state of the program, which is why void exists. The main method must be
           void, since the JRE does not expect a return value.
    
           "main" is the name of the method, and all Java programs must have one.
    
           "String[]" is an array of String classes, the String class is something you will
           quickly become familiar with. You may treat the string array parameter just like
           any other array of string.
    
           "args" is the name of the String class array parameter. A parameter is a data
           type or class reference that you pass to a method so that method may act upon
           that parameter and perform the programmed calculation in a different manner.
        */
    }
    I hope that all made sense!

    Quote Originally Posted by Did
    3. Last question I have is with this piece of code:
    Code:
    public void windowClosing(WindowEvent e)
    	{
    		System.exit(0);
    	}
    Now after WindowEvent, there is an e. Is this a variable? or like a parameter or something?
    That is actually pretty interesting, it's an overloaded method of the WindowListener interface. I'd like to see, in context, how this is applied, I'd imagine in the source code you're using it's from a temporary object called a "WindowAdapter". Please post more.

    To explain what "e" is, is that is both a parameter and a variable. It's also a class reference, a reference to a WindowEvent class. This is a callback method that refers to an event that is fired off when the window is closing, and the WindowEvent object has information regarding the old and new state of the window, the source object that fired the callback method from which the WindowEvent object was passed from (the source), and other identifying information. For the most part you can ignore this parameter unless you need to know what the source of the callback was, or if there's something about the state of the window you will need to know.

    I tried to be thorough, hope that was enough to get you started.
    Wow I changed my sig!

  5. #4
    Did
    Did is offline Newbie
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Re: Hey, first post! Very very basic java questions..

    Tells you how useful college is...
    Exactly. But I'm also going to a very ****ty school. They only have 1 computer degree and that is Computer Tech, and I hate hardware with a passion. I'm only going because its completely free (FAFSA). I'm transferring out of it soon though, hopefully. I'm looking towards a computer science degree.

    1.
    Alright so understand the asterisk I believe; basically if i had
    Code:
     -- Folder1
     ---File1
     ---File2
     ----Folder2
     -----File3
    and say I used import.folder1 itd only import file1 and file2 and it wouldn't import anything in any other folders such as File3. (Based on a file structure and not on packages. So I can see if I understand correctly)

    2.
    So Public, means other classes can use it..
    Private means that only that specific class can use it.
    In the code
    Code:
    private void doubleYear(){
        year*=2;
      }
    are you creating the method doubleYear, and then calling it later? Also if it was public and not private, wouldn't it not work since its void and as you said void method doesn't return something?

    Here:
    Code:
      public int getYear(){
        return year;
      }
    Is the method already created somewhere, and your just calling the method, and returning the variable year?
    If so is the getYear method in a package that I would have to import in the beginning?

    I understand(I think) the static way of doing things. She never really explained "new" etc, so MyClass test = new MyClass(); kinda confuses me.
    Fields/properties are also new to me and I don't quite understand them either. Are properties variables?

    As far as the main method goes, we've had to write a few applets and I don't believe I saw a main method in them? Is there a reason for that?

    The following is the code that you wanted me to post more of:
    Code:
     import java.awt.*;
    import java.awt.event.*;
    
    public class Buttons extends Frame implements ActionListener, ItemListener
    {
    	Choice colors = new Choice();
    	
    	public Buttons()
    	{
    		setLayout(new BorderLayout(20,5));
    		
    		colors.add("Red");
    		colors.add("Yellow");
    		colors.add("Magenta");
    		colors.add("Cyan");
    		colors.add("White");
    		colors.addItemListener(this);
    		
    		Button Red = new Button("Red");
    		Button Yellow = new Button("Yellow");
    		Button Magenta = new Button("Magenta");
    		Button Cyan = new Button("Cyan");
    		//Button White = new Button("White");
    		
    		add(Red, BorderLayout.NORTH);
    		add(Yellow, BorderLayout.SOUTH);
    		add(Magenta, BorderLayout.EAST);
    		add(Cyan, BorderLayout.WEST);
    		//add(White, BorderLayout.CENTER);
    		add(colors, BorderLayout.CENTER);
    		
    		Red.addActionListener(this);
    		Yellow.addActionListener(this);
    		Magenta.addActionListener(this);
    		Cyan.addActionListener(this);
    		//White.addActionListener(this);
    		
    		addWindowListener(
    			new WindowAdapter()
    				{
    				public void windowClosing(WindowEvent e)
    					{
    						System.exit(0);
    					}
    				}
    		);
    	}
    	
    	public static void main(String[] args)
    	{
    		Buttons f = new Buttons();
    		f.setTitle("Border Application");
    		f.setBounds(200,200,300,300);
    		f.setVisible(true);
    		f.setBackground(Color.red);
    	}
    	
    	public void actionPerformed(ActionEvent e)
    	{
    		String arg = e.getActionCommand();
    		
    		if (arg == "Red")
    			setBackground(Color.red);
    		
    		if (arg == "Yellow")
    			setBackground(Color.yellow);
    			
    		if (arg == "Magenta")
    			setBackground(Color.magenta);
    			
    		if (arg == "Cyan")
    			setBackground(Color.cyan);
    			
    	}
    	
    	public void itemStateChanged(ItemEvent ie)
    	{
    		String arg = colors.getSelectedItem();
    		
    		if (arg == "Red")
    			setBackground(Color.red);
    		
    		if (arg == "Yellow")
    			setBackground(Color.yellow);
    		
    		if (arg == "Magenta")
    			setBackground(Color.magenta);
    		
    		if (arg == "Cyan")
    			setBackground(Color.cyan);
    			
    		if (arg == "White")
    			setBackground(Color.white);
    	}
    	
    	
    }
    You both helped me out GREATLY, I understand a hell of a lot more than I once did. I spent a an hour or so just examining these posts.

    Also, I know a bit a of perl and a very very minimal amount of php and other stuff, so I guess Im trying to understand Java by comparing things.
    For example, methods to me seem like functions.


    P.S. I just happen to be eating spaghetti and meatballs tonight..(Your avatar)

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to create basic javascript page with random questions?
    By james345 in forum JavaScript and CSS
    Replies: 1
    Last Post: 10-19-2011, 05:55 PM
  2. A few basic questions about codecall...
    By exicute in forum The Lounge
    Replies: 5
    Last Post: 02-09-2010, 05:41 PM
  3. Some questions regarding java
    By ahmed in forum Java Help
    Replies: 3
    Last Post: 10-15-2009, 08:37 PM
  4. A few (basic) questions
    By Razzie89 in forum C and C++
    Replies: 1
    Last Post: 11-10-2007, 03:59 PM
  5. A few basic website questions
    By ehudgershon in forum General Programming
    Replies: 4
    Last Post: 06-11-2007, 12:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

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