|
Your understanding is a bit off.
There is no such thing, in Java at least, as a static object. There are classes with static only methods, so no instances are required, and there are singletons, classes which only ever have a single instance.
Each class defines a type of object and how it works, it is not the object itself. Within a class, you can have items defined as static. Static members are shared by all instances of the class and do not actually require an instance to be created; a good example is the main entry method.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Joshhua123 wrote: When creation of a Object which isn't static, each decleration of the object
with Object object = new Object() will create a new instance,
You can have static and non-static member variables. If you have non static member variable as per your example then for each instance of 'One' that you create then the member variable for that instance will point to a different instance of Object.
Joshhua123 wrote: if it was static wouldn't that mean instead of creating multiple instances,
instead of only existing as one instance.
If the member variable was static then there would be only one instance of Object regardless of how many new instances of 'One' you created.
Joshhua123 wrote: As a non static would they then be independent of each other?
As noted above (when it is a non-static member) there is a new Object created each time. They are different so yes they are independent.
|
|
|
|
|
In this code
int i =2;
System.out.print(" Run " + i+1 + " ");
It prints Run 21
Where I would have expected to print Run 3
Amara
'Now I understand how stupid I am, thanks for everybody's contribution.
modified 18-Aug-12 18:54pm.
|
|
|
|
|
The plus operator is left associative so I'd expect 21 as the result. 22 does not make any sense whatsoever. What happens when you put i + 1 in parenthesis like this: (i + 1).
Try that and think about associativity of operators and what it means to the process of evaluation/compilation.
Cheers!
"With sufficient thrust, pigs fly just fine."
Ross Callon, The Twelve Networking Truths, RFC1925
|
|
|
|
|
The reason you are not getting 'Run 3' is because Java is thinking about string concatination. As it is processing from left to right you would see:
"Run 2" + 1 + ""
But this is where it gets a bit strange, I would expect to see the number 1 printed next giving you:
"Run 21"
If you want to add two numbers you must use brackets to indicate priority:
System.out.print("Run "+ (i+1));
|
|
|
|
|
..if the system had these kind of "bugs" or "features", they would have been found by now, don't you think?
|
|
|
|
|
The others have already explained why it is so.
I would like to add into, that the + is an operator and is overloaded in the String class and also used different in the System.out.
Operators in Java[^]
|
|
|
|
|
int i =2;
System.out.print(" Run " +(i+1)+ " ");
This is the code. Keep the brackets.
|
|
|
|
|
suppose i have a text file as data.txt has following content such as
12.12.12.12 "www.google.com"|"www.yahoo.com"|"www.rediffmail.com"
13.13.12.12 "www.yahoo.com"|"www.rediffmail.com"|"www.google.com"|"www.google.com"
14.14.12.12 "www.yahoo.com"|"www.rediffmail.com"|"www.yahoo.com"
i have another text file as token.txt has following content such as
"www.google.com" 100
"www.yahoo.com" 200
"www.rediffmail.com" 300
i want to make new file as result.txt that should contain the following data such as
12.12.12.12 100 200 300
13.13.12.12 200 300 100 100
14.14.12.12 200 300 200
my code is as follows
import java.util.*;
import java.io.*;
public class UpdateFile
{
public static void main(String[] args) throws Exception
{
BufferedReader in=new BufferedReader(new FileReader(new File("data.txt")));
PrintWriter out=new PrintWriter(new FileWriter(new File("result.txt")));
String str="";
while((str=in.readLine())!=null)
{
ArrayList<string> url=new ArrayList<string>();
StringBuilder sq = new StringBuilder();
String parts[]=str.split("\\s+");
BufferedReader in2=new BufferedReader(new FileReader(new File("token.txt")));
String str2="";
String matchstr= parts[1];
String mm[]=matchstr.split("\\|");
for(int h =0;h<mm.length;h++){
while((str2="in2.readLine())!=null){
" string="" urltoken[]="str2.split("\\s+");
" if(mm[h]="=" null="" ?="" urltoken[0]="=" :="" mm[h].equals(urltoken[0])){
="" if(urltoken[1].tostring()!="null){
" url.add(urltoken[1].tostring()+"?");
=""
="" }
="" url.add(str2);
=""
="" if(url.size()="">0)
out.print(parts[0]+" "+" ");
for(int i=0;i<url.size();i++)
{
="" if(url.size()="">0)
{
out.print(url.get(i));
}}
url.clear();
out.println();
out.flush();
}
}
}
|
|
|
|
|
|
uppose i have a text file as data.txt has following content such as
12.12.12.12 "www.google.com"|"www.yahoo.com"|"www.rediffmail.com"
13.13.12.12 "www.yahoo.com"|"www.rediffmail.com"|"www.google.com"|"www.google.com"
14.14.12.12 "www.yahoo.com"|"www.rediffmail.com"|"www.yahoo.com"
i have another text file as token.txt has following content such as
"www.google.com" 100
"www.yahoo.com" 200
"www.rediffmail.com" 300
i want to make new file as result.txt that should contain the following data such as
12.12.12.12 100 200 300
13.13.12.12 200 300 100 100
14.14.12.12 200 300 200
my code is as follows
import java.util.*;
import java.io.*;
public class Remove
{
public static void main(String[] args) throws Exception
{
BufferedReader in=new BufferedReader(new FileReader(new File("C://find token//output.txt")));
PrintWriter out=new PrintWriter(new FileWriter(new File("C://find token//WASD.txt")));
//BufferedReader in=new BufferedReader(new FileReader(new File("C://find token//Nasa File Data//output.txt")));
//PrintWriter out=new PrintWriter(new FileWriter(new File("C://find token//Nasa File Data//WASD.txt")));
String str="";
while((str=in.readLine())!=null)
{
ArrayList<string> url=new ArrayList<string>();
String parts[]=str.split("\\s+");
BufferedReader in2=new BufferedReader(new FileReader(new File("C://find token//tokenfile.txt")));
String str2="";
//char []n= new char[27];
while((str2=in2.readLine())!=null)
{
String urltoken[]=str2.split("\\s+");
if(parts[1].indexOf(urltoken[0].toString())!=-1)
{
if(urltoken[1].toString()!=null)
url.add(urltoken[1].toString()+"?");
}
}
if(url.size()>0)
out.print(parts[0]+" "+" ");
//System.out.println(url.size());
for(int i=0;i<url.size();i++)
{
="" if(url.size()="">0)
{
out.print(url.get(i));
}}
in2.close();
url.clear();
out.println();
out.flush();
//}
}System.out.println("WASD is Created");
}
}
|
|
|
|
|
This is a repost of code for project work[^], which has already received some suggestions. If you want further help then please edit this question and put your code between <pre> tags for readability. Also, please explain what problems you are having with your code.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
|
First lets give some context, I've just created a client and server but they don't work without using console So I've decided to create a GUI on top they'll communicate though the Stack, but that's all for a later date, right now I get a error on the Even listener which says that the JTextField is empty upon .getText() even though it's not. I've looked though it, but since I'm quite new to the whole Swing field, it's a challenge for me to fix the problem. It really is a simple GUI.
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.*;
public class GUI extends JFrame implements ActionListener{
Stack<String> incoming = null;
Stack<String> outgoing = null;
JTextField input;
JTextArea output;
Button button;
public GUI(){
setTitle("Satire v1.0.1");
setSize(320,360);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null);
output = new JTextArea();
output.setBounds(0, 0, 300, 300);
output.setEditable(false);
input = new JTextField();
input.setBounds(0, 300, 260, 20);
button = new Button("SEND");
button.setBounds(250, 300, 50, 20);
button.addActionListener(this);
getContentPane().add(panel);
panel.add(button);
panel.add(input);
panel.add(output);
}
public void actionPerformed(ActionEvent e) {
GUI gui = new GUI();
try{
String out = gui.input.getText();
gui.input.selectAll();
outgoing.add(out);
}catch(NullPointerException e3){}
gui.output.append(outgoing.pop());
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run(){
GUI cGUI = new GUI();
cGUI.setVisible(true);
}
});
}
}
|
|
|
|
|
You are new to object oriented programming as well, arent you?
Instead of this:
public void actionPerformed(ActionEvent e) {
GUI gui = new GUI();
try{
String out = gui.input.getText();
gui.input.selectAll();
outgoing.add(out);
}catch(NullPointerException e3){}
gui.output.append(outgoing.pop());
}
Use this:
public void actionPerformed(ActionEvent e) {
try{
String out = input.getText();
input.selectAll();
outgoing.add(out);
}catch(NullPointerException e3){}
output.append(outgoing.pop());
}
|
|
|
|
|
I have made the edit, yes I'm new, but learning fast, a lot faster than expected.
though the edit has been made, there was no change to the error. What I don't understand is that the field isn't empty D:
|
|
|
|
|
There you go. This time I compiled/tested it, contained another big mistake. Why have you used those stacks? You should check the error messages as well in the console output!!! You program threw exceptions!
import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.*;
public class GUI extends JFrame implements ActionListener{
JTextField input;
JTextArea output;
Button button;
public GUI(){
setTitle("Satire v1.0.1");
setSize(320,360);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(null);
output = new JTextArea();
output.setBounds(0, 0, 300, 300);
output.setEditable(false);
input = new JTextField();
input.setBounds(0, 300, 260, 20);
button = new Button("SEND");
button.setBounds(250, 300, 50, 20);
button.addActionListener(this);
getContentPane().add(panel);
panel.add(button);
panel.add(input);
panel.add(output);
}
public void actionPerformed(ActionEvent e) {
try{
String out = input.getText();
output.append(input.getText());
input.selectAll();
input.requestFocusInWindow();
}catch(NullPointerException e3){}
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
public void run(){
GUI cGUI = new GUI();
cGUI.setVisible(true);
}
});
}
}
|
|
|
|
|
the purpose of the stack was for when I implements it into my chat client I'll have something to send it by, without changing the code around too much. I personally don't see how it caused an error.
|
|
|
|
|
It caused an error because in java every object is used by reference. You had there a null reference to a stack object, actually you haven't created your stack object and when you tried to use it that was a NullPointerException!
|
|
|
|
|
Meaning i should of put Stack<string> incoming = new Stack<string>();
|
|
|
|
|
Yep. Common mistake when starting java. In java only the primitive types are self contained, everything else is just a null or uninitialized reference without initialization.
|
|
|
|
|
Hopefully you could help me with this one, I've found the problem (Thanks to the breakpoints) My main created a thread1 and then thread1 created thread 2, both of which contain a while(true) (Then just sit and wait for input and output) but when thread1 creates thread2 it doesn't go past the line which creates thread2 which Isn't what I thought would happen... I thought Thread1 would create thread2 then proceeds to the loop for user input.
|
|
|
|
|
You are right that should happen.
|
|
|
|
|
I see no error but to call a implements Runnable I use the line,
(new Thread(new accept(br))).start(); if that helps
|
|
|
|
|
Not to be rude but maybe you should learn the basics first, correct threading is more or less an advanced topic. Actually the java tutorials put together by sun is exceptionally good: http://docs.oracle.com/javase/tutorial/[^]
There is a paragraph on that page with "Trails Covering the Basics" header, you should read through that carefully, you will find topics about threading in the "Essential Java Classes" part, but read it from the beginning to the end without skipping. After this basic tutorial you won't ask questions like this.
|
|
|
|